Skip to content

Commit

Permalink
🧹 window: move common actions to the helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jan 11, 2025
1 parent c7bd6c3 commit 6d32584
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ fn on_btn_clicked(param: &[glib::Value]) -> Option<glib::Value> {
let widget = param[0].get::<gtk::Button>().unwrap();
let name = widget.widget_name();

let builder_ref = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().builder };
let stack: &gtk::Stack = &builder_ref.object("stack").unwrap();
stack.set_visible_child_name(&format!("{name}page"));
let child_name = format!("{name}page");
unsafe { G_HELLO_WINDOW.as_ref().unwrap().set_stack_child_visible(&child_name) };

None
}
Expand All @@ -222,11 +221,10 @@ fn on_link_clicked(param: &[glib::Value]) -> Option<glib::Value> {
let widget = param[0].get::<gtk::Widget>().unwrap();
let name = widget.widget_name();

let window_ref = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().window };
let preferences = unsafe { G_HELLO_WINDOW.as_ref().unwrap().get_preferences("urls") };

let uri = preferences[name.as_str()].as_str().unwrap();
let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
unsafe { G_HELLO_WINDOW.as_ref().unwrap().open_uri(uri) };

None
}
Expand All @@ -235,11 +233,10 @@ fn on_link1_clicked(param: &[glib::Value]) -> Option<glib::Value> {
let widget = param[0].get::<gtk::Widget>().unwrap();
let name = widget.widget_name();

let window_ref = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().window };
let preferences = unsafe { G_HELLO_WINDOW.as_ref().unwrap().get_preferences("urls") };

let uri = preferences[name.as_str()].as_str().unwrap();
let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
unsafe { G_HELLO_WINDOW.as_ref().unwrap().open_uri(uri) };

Some(false.to_value())
}
Expand Down
11 changes: 11 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ impl HelloWindow {
}
}

pub fn open_uri(&self, uri: &str) {
if let Err(uri_err) = gtk::show_uri_on_window(Some(&self.window), uri, 0) {
error!("Failed to open uri: {uri_err}");
}
}

pub fn set_stack_child_visible(&self, child_name: &str) {
let stack: &gtk::Stack = &self.builder.object("stack").unwrap();
stack.set_visible_child_name(child_name);
}

pub fn get_preferences(&self, entry: &str) -> &serde_json::Value {
&self.preferences[entry]
}
Expand Down

0 comments on commit 6d32584

Please sign in to comment.