App launcher view
This commit is contained in:
@@ -1,22 +1,34 @@
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
use wgui::{
|
||||
assets::AssetPath,
|
||||
components::button::ComponentButton,
|
||||
components::button::{ButtonClickCallback, ComponentButton},
|
||||
globals::WguiGlobals,
|
||||
i18n::Translation,
|
||||
layout::WidgetPair,
|
||||
parser::{Fetchable, ParseDocumentParams, ParserData, ParserState},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
frontend::FrontendTask,
|
||||
frontend::{FrontendTask, RcFrontend},
|
||||
tab::{Tab, TabParams, TabType},
|
||||
util::{self, desktop_finder::DesktopEntry},
|
||||
views,
|
||||
util::{
|
||||
self,
|
||||
desktop_finder::DesktopEntry,
|
||||
popup_manager::{MountPopupParams, PopupHandle},
|
||||
},
|
||||
views::{self, app_launcher},
|
||||
};
|
||||
|
||||
struct State {
|
||||
launcher: Option<(PopupHandle, views::app_launcher::View)>,
|
||||
}
|
||||
|
||||
pub struct TabApps {
|
||||
#[allow(dead_code)]
|
||||
pub state: ParserState,
|
||||
pub parser_state: ParserState,
|
||||
|
||||
state: Rc<RefCell<State>>,
|
||||
|
||||
#[allow(dead_code)]
|
||||
entries: Vec<DesktopEntry>,
|
||||
@@ -35,6 +47,40 @@ struct AppList {
|
||||
data: Vec<ParserData>,
|
||||
}
|
||||
|
||||
// called after the user clicks any desktop entry
|
||||
fn on_app_click(
|
||||
frontend: RcFrontend,
|
||||
globals: WguiGlobals,
|
||||
entry: DesktopEntry,
|
||||
state: Rc<RefCell<State>>,
|
||||
) -> ButtonClickCallback {
|
||||
Box::new(move |_common, _evt| {
|
||||
frontend
|
||||
.borrow_mut()
|
||||
.tasks
|
||||
.push(FrontendTask::MountPopup(MountPopupParams {
|
||||
title: Translation::from_raw_text(&entry.app_name),
|
||||
on_content: {
|
||||
let state = state.clone();
|
||||
let entry = entry.clone();
|
||||
let globals = globals.clone();
|
||||
Box::new(move |data| {
|
||||
let view = app_launcher::View::new(app_launcher::Params {
|
||||
entry: entry.clone(),
|
||||
globals: globals.clone(),
|
||||
layout: data.layout,
|
||||
parent_id: data.id_content,
|
||||
})?;
|
||||
|
||||
state.borrow_mut().launcher = Some((data.handle, view));
|
||||
Ok(())
|
||||
})
|
||||
},
|
||||
}));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
impl TabApps {
|
||||
pub fn new(mut tab_params: TabParams) -> anyhow::Result<Self> {
|
||||
let doc_params = &ParseDocumentParams {
|
||||
@@ -43,21 +89,39 @@ impl TabApps {
|
||||
extra: Default::default(),
|
||||
};
|
||||
|
||||
let mut state = wgui::parser::parse_from_assets(doc_params, tab_params.layout, tab_params.parent_id)?;
|
||||
|
||||
gtk::init()?;
|
||||
|
||||
let entries = util::desktop_finder::find_entries()?;
|
||||
|
||||
let app_list_parent = state.fetch_widget(&tab_params.layout.state, "app_list_parent")?;
|
||||
let frontend = tab_params.frontend.clone();
|
||||
let globals = tab_params.globals.clone();
|
||||
|
||||
let state = Rc::new(RefCell::new(State { launcher: None }));
|
||||
|
||||
let mut parser_state = wgui::parser::parse_from_assets(doc_params, tab_params.layout, tab_params.parent_id)?;
|
||||
let app_list_parent = parser_state.fetch_widget(&tab_params.layout.state, "app_list_parent")?;
|
||||
let mut app_list = AppList::default();
|
||||
app_list.mount_entries(&entries, &mut state, doc_params, &mut tab_params, &app_list_parent)?;
|
||||
app_list.mount_entries(
|
||||
&entries,
|
||||
&mut parser_state,
|
||||
doc_params,
|
||||
&mut tab_params,
|
||||
&app_list_parent,
|
||||
|button, entry| {
|
||||
// Set up the click handler for the app button
|
||||
button.on_click(on_app_click(
|
||||
frontend.clone(),
|
||||
globals.clone(),
|
||||
entry.clone(),
|
||||
state.clone(),
|
||||
));
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
app_list,
|
||||
state,
|
||||
parser_state,
|
||||
entries,
|
||||
state,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -70,7 +134,7 @@ impl AppList {
|
||||
params: &mut TabParams,
|
||||
list_parent: &WidgetPair,
|
||||
entry: &DesktopEntry,
|
||||
) -> anyhow::Result<()> {
|
||||
) -> anyhow::Result<Rc<ComponentButton>> {
|
||||
let mut template_params = HashMap::new();
|
||||
|
||||
// entry icon
|
||||
@@ -95,20 +159,7 @@ impl AppList {
|
||||
template_params.insert(Rc::from("name"), Rc::from(entry.app_name.as_str()));
|
||||
|
||||
let data = parser_state.parse_template(doc_params, "AppEntry", params.layout, list_parent.id, template_params)?;
|
||||
|
||||
let button = data.fetch_component_as::<ComponentButton>("button")?;
|
||||
|
||||
button.on_click({
|
||||
let frontend = params.frontend.clone();
|
||||
Box::new(move |_common, _evt| {
|
||||
frontend.borrow_mut().push_task(FrontendTask::MountPopup);
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
self.data.push(data);
|
||||
|
||||
Ok(())
|
||||
data.fetch_component_as::<ComponentButton>("button")
|
||||
}
|
||||
|
||||
fn mount_entries(
|
||||
@@ -118,9 +169,11 @@ impl AppList {
|
||||
doc_params: &ParseDocumentParams,
|
||||
params: &mut TabParams,
|
||||
list_parent: &WidgetPair,
|
||||
on_button: impl Fn(Rc<ComponentButton>, &DesktopEntry),
|
||||
) -> anyhow::Result<()> {
|
||||
for entry in entries {
|
||||
self.mount_entry(parser_state, doc_params, params, list_parent, entry)?;
|
||||
let button = self.mount_entry(parser_state, doc_params, params, list_parent, entry)?;
|
||||
on_button(button, entry);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ impl TabType {
|
||||
pub fn register_button(this_rc: RcFrontend, btn: &Rc<ComponentButton>, tab: TabType) {
|
||||
btn.on_click({
|
||||
Box::new(move |_common, _evt| {
|
||||
this_rc.borrow_mut().push_task(FrontendTask::SetTab(tab));
|
||||
this_rc.borrow_mut().tasks.push(FrontendTask::SetTab(tab));
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
@@ -74,7 +74,7 @@ impl TabSettings {
|
||||
state.data.fetch_component_as::<ComponentCheckbox>("cb_am_pm_clock")?,
|
||||
|settings| &mut settings.general.am_pm_clock,
|
||||
Some(|frontend, _| {
|
||||
frontend.push_task(FrontendTask::RefreshClock);
|
||||
frontend.tasks.push(FrontendTask::RefreshClock);
|
||||
}),
|
||||
)?;
|
||||
|
||||
@@ -85,7 +85,7 @@ impl TabSettings {
|
||||
.fetch_component_as::<ComponentCheckbox>("cb_opaque_background")?,
|
||||
|settings| &mut settings.general.opaque_background,
|
||||
Some(|frontend, _| {
|
||||
frontend.push_task(FrontendTask::RefreshBackground);
|
||||
frontend.tasks.push(FrontendTask::RefreshBackground);
|
||||
}),
|
||||
)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user