Skip to content

Commit

Permalink
feat: refresh content when window shown
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Dec 9, 2023
1 parent 8140a13 commit 20b6101
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/src/ast/variable_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ impl VariableExpression {
}
}

#[inline]
pub fn name(&self) -> &StString {
&self.name
}

/// Origin name of the variable
#[inline]
pub fn org_name(&self) -> &String {
self.name().origin_string()
}

#[inline]
pub fn ty(&self) -> Option<Rc<Box<dyn Type>>> {
self.ty.clone()
}

#[inline]
pub fn set_ty(&mut self, ty: Option<Rc<Box<dyn Type>>>) {
self.ty = ty
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<W: Write> AstVisitor<'_> for StringifyVisitor<W> {
}

fn visit_variable_expression(&mut self, variable: &'_ VariableExpression) {
self.write(format_args!("{}", variable.name().origin_string()));
self.write(format_args!("{}", variable.org_name()));
}

fn visit_call_expression(&mut self, call: &'_ CallExpression) {
Expand Down
27 changes: 27 additions & 0 deletions viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod column_object;
mod stc_viewer;

use crate::stc_viewer::{StcViewerApp, STC_VIEWER_COLUMN_NAME};
use glib::{ControlFlow, MainContext};
use gtk::prelude::*;
use gtk::{
Adjustment, Application, ApplicationWindow, CellRendererText, Orientation, Paned,
Expand All @@ -12,6 +13,11 @@ use stc::prelude::*;
use std::rc::Rc;
use std::sync::Mutex;

/// Send UI operations from other threads
enum UIMessages {
Refresh,
}

fn main() {
pretty_env_logger::init();

Expand Down Expand Up @@ -138,6 +144,27 @@ fn build_ui(app: &Application, mgr: UnitsManager) {
}
});

let (tx, rx) = MainContext::channel::<UIMessages>(glib::Priority::DEFAULT);

// refresh UI when the window is shown
window.connect_show(move |_| {
let tx = tx.clone();
glib::idle_add(move || {
tx.send(UIMessages::Refresh).unwrap();
ControlFlow::Break
});
});

// handle UI messages from other threads
let app_copy = stc_app.clone();
rx.attach(None, move |msg| {
match msg {
UIMessages::Refresh => app_copy.lock().unwrap().refresh(),
};

ControlFlow::Continue
});

window.add(&paned);
window.show_all();
}

0 comments on commit 20b6101

Please sign in to comment.