feat(app): integrate ContextDrawer with context_drawer method

This commit is contained in:
Michael Aaron Murphy 2023-10-12 13:23:59 +02:00 committed by Michael Murphy
parent 3127de3296
commit d620531e7e
3 changed files with 36 additions and 8 deletions

View file

@ -15,11 +15,14 @@ pub struct NavBar {
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone)]
pub struct Window {
/// Label to as title in headerbar.
/// Label to display as context drawer title.
pub context_title: String,
/// Label to display as header bar title.
pub header_title: String,
pub use_template: bool,
pub can_fullscreen: bool,
pub sharp_corners: bool,
pub show_context: bool,
pub show_headerbar: bool,
pub show_window_menu: bool,
pub show_maximize: bool,
@ -68,10 +71,12 @@ impl Default for Core {
title: String::new(),
system_theme: crate::theme::active(),
window: Window {
context_title: String::new(),
header_title: String::new(),
use_template: true,
can_fullscreen: false,
sharp_corners: false,
show_context: false,
show_headerbar: true,
show_maximize: true,
show_minimize: true,

View file

@ -24,6 +24,8 @@ pub enum Message {
AppThemeChange(Theme),
/// Requests to close the window.
Close,
/// Closes or shows the context drawer.
ContextDrawer(bool),
/// Requests to drag the window.
Drag,
/// Keyboard shortcuts managed by libcosmic.
@ -166,6 +168,7 @@ where
if id != window::Id(0) {
return self.app.view_window(id).map(super::Message::App);
}
if self.app.core().window.use_template {
self.app.view_main()
} else {
@ -242,13 +245,12 @@ impl<T: Application> Cosmic<T> {
keyboard_nav::Message::Fullscreen => return command::toggle_fullscreen(),
},
Message::Drag => return command::drag(),
Message::Close => {
self.app.on_app_exit();
return self.close();
Message::ContextDrawer(show) => {
self.app.core_mut().window.show_context = show;
}
Message::Drag => return command::drag(),
Message::Minimize => return command::minimize(),
Message::Maximize => {
@ -303,6 +305,11 @@ impl<T: Application> Cosmic<T> {
Message::ScaleFactor(factor) => {
self.app.core_mut().set_scale_factor(factor);
}
Message::Close => {
self.app.on_app_exit();
return self.close();
}
}
iced::Command::none()

View file

@ -41,7 +41,7 @@ pub use self::core::Core;
pub use self::settings::Settings;
use crate::prelude::*;
use crate::theme::THEME;
use crate::widget::nav_bar;
use crate::widget::{context_drawer, nav_bar};
use apply::Apply;
use iced::Subscription;
use iced::{window, Application as IcedApplication};
@ -62,6 +62,7 @@ pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Res
core.set_scale_factor(settings.scale_factor);
core.set_window_width(settings.size.0);
core.set_window_height(settings.size.1);
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
cosmic_theme.set_theme(settings.theme.theme_type);
@ -139,6 +140,11 @@ where
/// Creates the application, and optionally emits command on initialize.
fn init(core: Core, flags: Self::Flags) -> (Self, iced::Command<Message<Self::Message>>);
/// Displays a context drawer on the side of the application window when `Some`.
fn context_drawer(&self) -> Option<Element<Self::Message>> {
None
}
/// Attaches elements to the start section of the header.
fn header_start(&self) -> Vec<Element<Self::Message>> {
Vec::new()
@ -332,7 +338,17 @@ impl<App: Application> ApplicationExt for App {
if self.nav_model().is_none() || core.show_content() {
let main_content = self.view().debug(core.debug).map(Message::App);
widgets.push(main_content);
widgets.push(if let Some(context) = self.context_drawer() {
context_drawer(
&core.window.context_title,
Message::Cosmic(cosmic::Message::ContextDrawer(false)),
main_content,
context.map(Message::App),
)
.into()
} else {
main_content
});
}
widgets