From cc0530471f321ed1bf3b232f3d210f446dd2ed6a Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sun, 2 Jul 2017 19:08:17 +0200 Subject: [PATCH] Update --- src/alsa_pn.rs | 4 +- src/app_state.rs | 19 ++------- src/ui_entry.rs | 1 - src/ui_popup_menu.rs | 13 ++---- src/ui_prefs_dialog.rs | 93 ++++++++++++++++++++++++++---------------- 5 files changed, 67 insertions(+), 63 deletions(-) diff --git a/src/alsa_pn.rs b/src/alsa_pn.rs index 873aaaee3..46139767e 100644 --- a/src/alsa_pn.rs +++ b/src/alsa_pn.rs @@ -311,9 +311,7 @@ pub fn get_selems(mixer: &Mixer) -> Map Selem> { pub fn get_selem_names(mixer: &Mixer) -> Vec { let mut vec = vec![]; for selem in get_selems(mixer) { - let n = selem.get_id() - .get_name() - .map(|y| String::from(y)); + let n = selem.get_id().get_name().map(|y| String::from(y)); match n { Ok(name) => vec.push(name), _ => (), diff --git a/src/app_state.rs b/src/app_state.rs index 189358dae..1f9f3bc5f 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -17,12 +17,9 @@ impl AppS { let builder_popup_window = gtk::Builder::new_from_string(include_str!("../data/ui/popup-window.glade")); let builder_popup_menu = gtk::Builder::new_from_string(include_str!("../data/ui/popup-menu.glade")); - let builder_prefs_dialog = - gtk::Builder::new_from_string(include_str!("../data/ui/prefs-dialog.glade")); + return AppS { - gui: Gui::new(builder_popup_window, - builder_popup_menu, - builder_prefs_dialog), + gui: Gui::new(builder_popup_window, builder_popup_menu), audio: Audio::new(None, Some(String::from("Master"))) .unwrap(), }; @@ -34,20 +31,18 @@ pub struct Gui { pub status_icon: gtk::StatusIcon, pub popup_window: PopupWindow, pub popup_menu: PopupMenu, - pub prefs_dialog: PrefsDialog, + /* prefs_dialog is dynamically created and destroyed */ } impl Gui { pub fn new(builder_popup_window: gtk::Builder, - builder_popup_menu: gtk::Builder, - builder_prefs_dialog: gtk::Builder) + builder_popup_menu: gtk::Builder) -> Gui { return Gui { status_icon: gtk::StatusIcon::new_from_icon_name("pnmixer"), popup_window: PopupWindow::new(builder_popup_window), popup_menu: PopupMenu::new(builder_popup_menu), - prefs_dialog: PrefsDialog::new(builder_prefs_dialog), }; } } @@ -66,9 +61,3 @@ create_builder_item!(PopupMenu, menu: gtk::Menu, about_item: gtk::MenuItem, prefs_item: gtk::MenuItem); - - -create_builder_item!(PrefsDialog, - prefs_dialog: gtk::Dialog, - card_combo: gtk::ComboBoxText, - chan_combo: gtk::ComboBoxText); diff --git a/src/ui_entry.rs b/src/ui_entry.rs index d182c99e3..2c980b70e 100644 --- a/src/ui_entry.rs +++ b/src/ui_entry.rs @@ -26,5 +26,4 @@ pub fn init(appstate: Rc) { init_tray_icon(appstate.clone()); init_popup_window(appstate.clone()); init_popup_menu(appstate.clone()); - init_prefs_dialog(appstate.clone()); } diff --git a/src/ui_popup_menu.rs b/src/ui_popup_menu.rs index 1fa37814f..0fa8a7269 100644 --- a/src/ui_popup_menu.rs +++ b/src/ui_popup_menu.rs @@ -2,6 +2,7 @@ use app_state::*; use gtk::prelude::*; use std::rc::Rc; use gtk; +use ui_prefs_dialog::*; @@ -24,13 +25,12 @@ pub fn init_popup_menu(appstate: Rc) { /* about_item.connect_activate_link */ { - let apps = appstate.clone(); let prefs_item = &appstate.clone() .gui .popup_menu .prefs_item; prefs_item.connect_activate(move |_| { - on_prefs_item_activate(&apps); + on_prefs_item_activate(appstate.clone()); }); } } @@ -74,12 +74,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")); } -fn on_prefs_item_activate(appstate: &AppS) { +fn on_prefs_item_activate(appstate: Rc) { /* TODO: only create if needed */ - let prefs_dialog = &appstate.gui.prefs_dialog.prefs_dialog; - let popup_menu = &appstate.gui.popup_menu.menu_window; - - prefs_dialog.set_transient_for(popup_menu); - prefs_dialog.run(); - // prefs_dialog.destroy(); + show_prefs_dialog(appstate); } diff --git a/src/ui_prefs_dialog.rs b/src/ui_prefs_dialog.rs index 3929f919d..eb97e9fa1 100644 --- a/src/ui_prefs_dialog.rs +++ b/src/ui_prefs_dialog.rs @@ -5,37 +5,63 @@ use gtk; use alsa_pn; -pub fn init_prefs_dialog(appstate: Rc) { - /* prefs_dialog.connect_show */ - { - let apps = appstate.clone(); - let prefs_dialog = &appstate.clone() - .gui - .prefs_dialog - .prefs_dialog; - prefs_dialog.connect_show(move |_| { on_prefs_dialog_show(&apps); }); - } +create_builder_item!(PrefsDialog, + prefs_dialog: gtk::Dialog, + card_combo: gtk::ComboBoxText, + chan_combo: gtk::ComboBoxText); - /* card_combo.connect_changed */ - { - let apps = appstate.clone(); - let card_combo = &appstate.gui.prefs_dialog.card_combo; - card_combo.connect_changed(move |_| { on_card_combo_changed(&apps); }); - } - /* card_combo.connect_changed */ - { - let apps = appstate.clone(); - let chan_combo = &appstate.gui.prefs_dialog.chan_combo; - chan_combo.connect_changed(move |_| { on_chan_combo_changed(&apps); }); + +pub fn show_prefs_dialog(appstate: Rc) { + let builder_prefs_dialog = gtk::Builder::new_from_string(include_str!("../data/ui/prefs-dialog.glade")); + let prefs_dialog = Rc::new(PrefsDialog::new(builder_prefs_dialog)); + init_prefs_dialog(&appstate, &prefs_dialog); + { + let prefs_dialog_w = &prefs_dialog.prefs_dialog; + prefs_dialog_w.set_transient_for(&appstate.gui.popup_menu.menu_window); + prefs_dialog_w.run(); + prefs_dialog_w.destroy(); } } -fn on_prefs_dialog_show(appstate: &AppS) { - let card_combo = &appstate.gui.prefs_dialog.card_combo; - let chan_combo = &appstate.gui.prefs_dialog.chan_combo; +/* TODO: do the references get dropped when the dialog window is gone? */ +pub fn init_prefs_dialog(appstate: &Rc, prefs_dialog: &Rc) { + + /* prefs_dialog.connect_show */ + { + let apps = appstate.clone(); + let pd = prefs_dialog.clone(); + prefs_dialog.clone().prefs_dialog.connect_show(move |_| { on_prefs_dialog_show(&apps, &pd); }); + } + + /* card_combo.connect_changed */ + { + let apps = appstate.clone(); + let card_combo = &prefs_dialog.card_combo; + let pd = prefs_dialog.clone(); + + card_combo.connect_changed(move |_| { + on_card_combo_changed(&apps, &pd); + }); + } + /* card_combo.connect_changed */ + { + let apps = appstate.clone(); + let chan_combo = &prefs_dialog.chan_combo; + let pd = prefs_dialog.clone(); + + chan_combo.connect_changed(move |_| { + on_chan_combo_changed(&apps, &pd); + }); + } +} + + +fn on_prefs_dialog_show(appstate: &AppS, prefs_dialog: &PrefsDialog) { + let card_combo = &prefs_dialog.card_combo; + let chan_combo = &prefs_dialog.chan_combo; let acard = appstate.audio.acard.borrow(); @@ -79,17 +105,15 @@ fn on_prefs_dialog_show(appstate: &AppS) { } -fn on_card_combo_changed(appstate: &AppS) { - let card_combo = &appstate.gui.prefs_dialog.card_combo; - let chan_combo = &appstate.gui.prefs_dialog.chan_combo; +fn on_card_combo_changed(appstate: &AppS, prefs_dialog: &PrefsDialog) { + let card_combo = &prefs_dialog.card_combo; + let chan_combo = &prefs_dialog.chan_combo; let active_card_item = try_w!(card_combo.get_active_text().ok_or("No active Card item found")); - let active_chan_item = - chan_combo.get_active_id(); + let active_chan_item = chan_combo.get_active_id(); let cur_card_name = { let acard = appstate.audio.acard.borrow(); - try_w!(acard.card_name(), - "Can't get current card name!") + try_w!(acard.card_name(), "Can't get current card name!") }; if active_card_item != cur_card_name { @@ -98,9 +122,9 @@ fn on_card_combo_changed(appstate: &AppS) { } -fn on_chan_combo_changed(appstate: &AppS) { - let card_combo = &appstate.gui.prefs_dialog.card_combo; - let chan_combo = &appstate.gui.prefs_dialog.chan_combo; +fn on_chan_combo_changed(appstate: &AppS, prefs_dialog: &PrefsDialog) { + let card_combo = &prefs_dialog.card_combo; + let chan_combo = &prefs_dialog.chan_combo; let active_chan_item = try_w!(chan_combo.get_active_text().ok_or("No active Chan item found")); let cur_card_name = { @@ -116,4 +140,3 @@ fn on_chan_combo_changed(appstate: &AppS) { appstate.audio.switch_acard(cur_card_name, Some(active_chan_item)); } } -