mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-09-08 21:01:53 +00:00
use shared update_actions
method
This commit is contained in:
parent
0f89b526f7
commit
0060bda5d2
@ -6,7 +6,7 @@ mod menu;
|
|||||||
mod widget;
|
mod widget;
|
||||||
|
|
||||||
use action::Action;
|
use action::Action;
|
||||||
use adw::TabPage;
|
use adw::{TabPage, TabView};
|
||||||
use error::Error;
|
use error::Error;
|
||||||
pub use item::Item;
|
pub use item::Item;
|
||||||
use menu::Menu;
|
use menu::Menu;
|
||||||
@ -42,7 +42,7 @@ impl Tab {
|
|||||||
let action = Rc::new(Action::new());
|
let action = Rc::new(Action::new());
|
||||||
|
|
||||||
// Init empty HashMap index
|
// Init empty HashMap index
|
||||||
let index: Rc<RefCell<HashMap<TabPage, Rc<Item>>>> = Rc::new(RefCell::new(HashMap::new()));
|
let index = Rc::new(RefCell::new(HashMap::new()));
|
||||||
|
|
||||||
// Init context menu
|
// Init context menu
|
||||||
let menu = Menu::new(window_action);
|
let menu = Menu::new(window_action);
|
||||||
@ -52,54 +52,30 @@ impl Tab {
|
|||||||
|
|
||||||
// Init events
|
// Init events
|
||||||
widget.tab_view.connect_setup_menu({
|
widget.tab_view.connect_setup_menu({
|
||||||
let action = window_action.clone();
|
|
||||||
let index = index.clone();
|
let index = index.clone();
|
||||||
|
let window_action = window_action.clone();
|
||||||
move |tab_view, tab_page| {
|
move |tab_view, tab_page| {
|
||||||
// Update actions on open popover request (`tab_page` == `Some`)
|
// by documentation:
|
||||||
// * it's no handle for popover close event (`tab_page` == `None`)
|
// * `tab_page` == `Some` - popover open
|
||||||
// as it will be updated on the next init for new tab selected
|
// * `tab_page` == `None` - popover closed
|
||||||
tab_page.map(|tab_page| {
|
update_actions(tab_view, tab_page.cloned(), &index, &window_action);
|
||||||
index.borrow().get(tab_page).map(|item| {
|
|
||||||
// Update enabled status (for some actions)
|
|
||||||
action
|
|
||||||
.history_back
|
|
||||||
.simple_action
|
|
||||||
.set_enabled(item.action.history.back.is_enabled());
|
|
||||||
action
|
|
||||||
.history_forward
|
|
||||||
.simple_action
|
|
||||||
.set_enabled(item.action.history.forward.is_enabled());
|
|
||||||
action
|
|
||||||
.home
|
|
||||||
.simple_action
|
|
||||||
.set_enabled(item.action.home.is_enabled());
|
|
||||||
action
|
|
||||||
.reload
|
|
||||||
.simple_action
|
|
||||||
.set_enabled(item.action.reload.is_enabled());
|
|
||||||
|
|
||||||
// Update target state (for all actions)
|
|
||||||
action.change_state(Some(tab_view.page_position(tab_page)));
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
widget.tab_view.connect_close_page({
|
widget.tab_view.connect_close_page({
|
||||||
let index = index.clone();
|
let index = index.clone();
|
||||||
let profile = profile.clone();
|
let profile = profile.clone();
|
||||||
move |_, tab_page| {
|
let window_action = window_action.clone();
|
||||||
// Cleanup HashMap index
|
move |tab_view, tab_page| {
|
||||||
if let Some(item) = index.borrow_mut().remove(tab_page) {
|
// cleanup HashMap index
|
||||||
// Add history record into profile memory pool
|
// add history record into profile memory pool
|
||||||
// * this action allows to recover recently closed tab (e.g. from the main menu)
|
// * this action allows to recover recently closed tab (e.g. from the main menu)
|
||||||
profile
|
profile.history.memory.tab.add(
|
||||||
.history
|
index.borrow_mut().remove(tab_page).unwrap(),
|
||||||
.memory
|
DateTime::now_local().unwrap().to_unix(),
|
||||||
.tab
|
);
|
||||||
.add(item, DateTime::now_local().unwrap().to_unix());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
update_actions(tab_view, tab_view.selected_page(), &index, &window_action);
|
||||||
Propagation::Proceed
|
Propagation::Proceed
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -107,21 +83,24 @@ impl Tab {
|
|||||||
widget.tab_view.connect_page_attached({
|
widget.tab_view.connect_page_attached({
|
||||||
let window_action = window_action.clone();
|
let window_action = window_action.clone();
|
||||||
let index = index.clone();
|
let index = index.clone();
|
||||||
move |_, tab_page, _| {
|
move |tab_view, _, _| {
|
||||||
if tab_page.is_selected() {
|
update_actions(tab_view, tab_view.selected_page(), &index, &window_action)
|
||||||
update_actions(tab_page, &index, &window_action);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
widget.tab_view.connect_selected_page_notify({
|
widget.tab_view.connect_selected_page_notify({
|
||||||
let window_action = window_action.clone();
|
let window_action = window_action.clone();
|
||||||
let index = index.clone();
|
let index = index.clone();
|
||||||
move |this| {
|
move |tab_view| {
|
||||||
if let Some(tab_page) = this.selected_page() {
|
update_actions(
|
||||||
tab_page.set_needs_attention(false);
|
tab_view,
|
||||||
update_actions(&tab_page, &index, &window_action);
|
tab_view.selected_page().map(|tab_page| {
|
||||||
}
|
tab_page.set_needs_attention(false);
|
||||||
|
tab_page
|
||||||
|
}),
|
||||||
|
&index,
|
||||||
|
&window_action,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -385,35 +364,45 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update_actions(
|
fn update_actions(
|
||||||
tab_page: &TabPage,
|
tab_view: &TabView,
|
||||||
|
tab_page: Option<TabPage>,
|
||||||
index: &Rc<RefCell<HashMap<TabPage, Rc<Item>>>>,
|
index: &Rc<RefCell<HashMap<TabPage, Rc<Item>>>>,
|
||||||
window_action: &Rc<WindowAction>,
|
window_action: &Rc<WindowAction>,
|
||||||
) {
|
) {
|
||||||
if let Some(item) = index.borrow().get(tab_page) {
|
match tab_page {
|
||||||
window_action
|
Some(tab_page) => {
|
||||||
.home
|
if let Some(item) = index.borrow().get(&tab_page) {
|
||||||
.simple_action
|
window_action
|
||||||
.set_enabled(item.action.home.is_enabled());
|
.home
|
||||||
window_action
|
.simple_action
|
||||||
.reload
|
.set_enabled(item.action.home.is_enabled());
|
||||||
.simple_action
|
window_action
|
||||||
.set_enabled(item.action.reload.is_enabled());
|
.reload
|
||||||
window_action
|
.simple_action
|
||||||
.history_back
|
.set_enabled(item.action.reload.is_enabled());
|
||||||
.simple_action
|
window_action
|
||||||
.set_enabled(item.action.history.back.is_enabled());
|
.history_back
|
||||||
window_action
|
.simple_action
|
||||||
.history_forward
|
.set_enabled(item.action.history.back.is_enabled());
|
||||||
.simple_action
|
window_action
|
||||||
.set_enabled(item.action.history.forward.is_enabled());
|
.history_forward
|
||||||
return;
|
.simple_action
|
||||||
}
|
.set_enabled(item.action.history.forward.is_enabled());
|
||||||
|
|
||||||
window_action.home.simple_action.set_enabled(false);
|
window_action.change_state(Some(tab_view.page_position(&tab_page)));
|
||||||
window_action.reload.simple_action.set_enabled(false);
|
} // @TODO incorrect index init implementation, tabs refactory wanted
|
||||||
window_action.history_back.simple_action.set_enabled(false);
|
}
|
||||||
window_action
|
None => {
|
||||||
.history_forward
|
// Reset to defaults
|
||||||
.simple_action
|
window_action.home.simple_action.set_enabled(false);
|
||||||
.set_enabled(false);
|
window_action.reload.simple_action.set_enabled(false);
|
||||||
|
window_action.history_back.simple_action.set_enabled(false);
|
||||||
|
window_action
|
||||||
|
.history_forward
|
||||||
|
.simple_action
|
||||||
|
.set_enabled(false);
|
||||||
|
|
||||||
|
window_action.change_state(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user