libcosmic/examples/cosmic/src/window.rs

608 lines
20 KiB
Rust
Raw Normal View History

2022-12-21 11:08:21 -07:00
/// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use cosmic::{
cosmic_theme::{
palette::{rgb::Rgb, Srgba},
ThemeBuilder,
},
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
font::load_fonts,
iced::{self, Application, Command, Length, Subscription},
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
iced::{
subscription,
widget::{self, column, container, horizontal_space, row, text},
window::{self, close, drag, minimize, toggle_maximize},
},
keyboard_nav,
prelude::*,
theme::{self, Theme},
widget::{
button, header_bar, icon, list, nav_bar, nav_bar_toggle, scrollable, segmented_button,
settings, warning,
},
Element,
2022-12-28 12:42:28 +01:00
};
2023-07-17 11:52:07 -04:00
use cosmic_time::{Instant, Timeline};
2022-12-28 12:42:28 +01:00
use std::{
2023-07-17 11:52:07 -04:00
cell::RefCell,
rc::Rc,
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
sync::{
atomic::{AtomicU32, Ordering},
Arc,
},
2022-12-28 12:42:28 +01:00
vec,
};
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
// XXX The use of button is removed because it assigns the same ID to multiple buttons, causing a crash when a11y is enabled...
// static BTN: Lazy<id::Id> = Lazy::new(|| id::Id::new("BTN"));
2023-01-05 11:29:14 -07:00
2022-12-21 13:11:32 -07:00
mod bluetooth;
2022-12-21 11:39:49 -07:00
2022-12-21 13:11:32 -07:00
mod demo;
2022-12-21 11:39:49 -07:00
2023-02-27 19:56:53 -05:00
use self::desktop::DesktopPage;
2022-12-21 13:11:32 -07:00
mod desktop;
2022-12-21 11:39:49 -07:00
mod editor;
2022-12-21 13:11:32 -07:00
use self::input_devices::InputDevicesPage;
mod input_devices;
2022-12-21 11:39:49 -07:00
2022-12-21 13:11:32 -07:00
use self::networking::NetworkingPage;
mod networking;
2022-12-21 11:39:49 -07:00
2022-12-21 13:11:32 -07:00
use self::system_and_accounts::SystemAndAccountsPage;
mod system_and_accounts;
2022-12-21 11:08:21 -07:00
2022-12-21 13:11:32 -07:00
use self::time_and_language::TimeAndLanguagePage;
mod time_and_language;
2022-12-21 11:08:21 -07:00
2022-12-21 13:11:32 -07:00
pub trait SubPage {
fn title(&self) -> &'static str;
fn description(&self) -> &'static str;
fn icon_name(&self) -> &'static str;
fn parent_page(&self) -> Page;
fn into_page(self) -> Page;
2022-12-21 09:27:10 -07:00
}
2022-12-20 16:02:44 -07:00
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2022-12-20 14:38:32 -07:00
pub enum Page {
Demo,
Editor,
2022-12-20 14:38:32 -07:00
WiFi,
2022-12-21 11:39:49 -07:00
Networking(Option<NetworkingPage>),
2022-12-20 14:38:32 -07:00
Bluetooth,
2022-12-21 11:08:21 -07:00
Desktop(Option<DesktopPage>),
2022-12-21 11:39:49 -07:00
InputDevices(Option<InputDevicesPage>),
2022-12-20 14:38:32 -07:00
Displays,
PowerAndBattery,
Sound,
PrintersAndScanners,
PrivacyAndSecurity,
2022-12-21 11:08:21 -07:00
SystemAndAccounts(Option<SystemAndAccountsPage>),
2022-12-20 14:38:32 -07:00
UpdatesAndRecovery,
2022-12-21 11:08:21 -07:00
TimeAndLanguage(Option<TimeAndLanguagePage>),
2022-12-20 14:38:32 -07:00
Accessibility,
Applications,
}
2022-12-21 09:27:10 -07:00
impl Page {
//TODO: translate
pub fn title(&self) -> &'static str {
use Page::*;
match self {
Demo => "Demo",
Editor => "Editor",
2022-12-21 09:27:10 -07:00
WiFi => "Wi-Fi",
2022-12-21 11:39:49 -07:00
Networking(_) => "Networking",
2022-12-21 09:27:10 -07:00
Bluetooth => "Bluetooth",
Desktop(_) => "Desktop",
2022-12-21 11:39:49 -07:00
InputDevices(_) => "Input Devices",
2022-12-21 09:27:10 -07:00
Displays => "Displays",
PowerAndBattery => "Power & Battery",
Sound => "Sound",
PrintersAndScanners => "Printers & Scanners",
PrivacyAndSecurity => "Privacy & Security",
2022-12-21 11:08:21 -07:00
SystemAndAccounts(_) => "System & Accounts",
2022-12-21 09:27:10 -07:00
UpdatesAndRecovery => "Updates & Recovery",
2022-12-21 11:08:21 -07:00
TimeAndLanguage(_) => "Time & Language",
2022-12-21 09:27:10 -07:00
Accessibility => "Accessibility",
Applications => "Applications",
}
}
2022-12-21 11:08:21 -07:00
pub fn icon_name(&self) -> &'static str {
use Page::*;
match self {
Demo => "document-properties-symbolic",
Editor => "text-editor-symbolic",
2022-12-21 11:08:21 -07:00
WiFi => "network-wireless-symbolic",
2022-12-21 11:39:49 -07:00
Networking(_) => "network-workgroup-symbolic",
2022-12-21 11:08:21 -07:00
Bluetooth => "bluetooth-active-symbolic",
Desktop(_) => "video-display-symbolic",
2022-12-21 11:39:49 -07:00
InputDevices(_) => "input-keyboard-symbolic",
2022-12-21 11:08:21 -07:00
Displays => "preferences-desktop-display-symbolic",
PowerAndBattery => "battery-full-charged-symbolic",
Sound => "multimedia-volume-control-symbolic",
PrintersAndScanners => "printer-symbolic",
PrivacyAndSecurity => "preferences-system-privacy-symbolic",
SystemAndAccounts(_) => "system-users-symbolic",
UpdatesAndRecovery => "software-update-available-symbolic",
TimeAndLanguage(_) => "preferences-system-time-symbolic",
Accessibility => "preferences-desktop-accessibility-symbolic",
Applications => "preferences-desktop-apps-symbolic",
}
}
2022-12-21 09:27:10 -07:00
}
2022-12-20 16:02:44 -07:00
impl Default for Page {
fn default() -> Page {
//TODO: what should the default page be?
2022-12-21 13:11:32 -07:00
Page::Desktop(None)
2022-12-20 16:02:44 -07:00
}
}
static WINDOW_WIDTH: AtomicU32 = AtomicU32::new(0);
const BREAK_POINT: u32 = 900;
#[derive(Default)]
pub struct Window {
bluetooth: bluetooth::State,
debug: bool,
demo: demo::State,
editor: editor::State,
desktop: desktop::State,
nav_bar: segmented_button::SingleSelectModel,
nav_id_to_page: segmented_button::SecondaryMap<Page>,
nav_bar_toggled_condensed: bool,
nav_bar_toggled: bool,
page: Page,
show_maximize: bool,
show_minimize: bool,
system_and_accounts: system_and_accounts::State,
theme: Theme,
title: String,
2023-01-16 12:04:39 -07:00
show_warning: bool,
warning_message: String,
scale_factor: f64,
scale_factor_string: String,
2023-07-17 11:52:07 -04:00
timeline: Rc<RefCell<Timeline>>,
}
impl Window {
pub fn nav_bar_toggled(mut self, toggled: bool) -> Self {
self.nav_bar_toggled = toggled;
self
}
pub fn show_maximize(mut self, show: bool) -> Self {
self.show_maximize = show;
self
}
pub fn show_minimize(mut self, show: bool) -> Self {
self.show_minimize = show;
self
}
2023-01-16 12:04:39 -07:00
pub fn show_warning(mut self, show: bool) -> Self {
self.show_warning = show;
self
}
}
#[allow(dead_code)]
#[derive(Clone, Debug)]
pub enum Message {
Bluetooth(bluetooth::Message),
Close,
2023-01-05 11:29:14 -07:00
CondensedViewToggle,
Demo(demo::Message),
Desktop(desktop::Message),
Drag,
Editor(editor::Message),
2022-11-22 17:51:09 -05:00
InputChanged,
KeyboardNav(keyboard_nav::Message),
2022-12-28 12:42:28 +01:00
Maximize,
Minimize,
NavBar(segmented_button::Entity),
2022-12-28 12:42:28 +01:00
Page(Page),
ToggleNavBar,
ToggleNavBarCondensed,
ToggleWarning,
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
FontsLoaded,
2023-07-17 11:52:07 -04:00
Tick(Instant),
}
impl From<Page> for Message {
fn from(page: Page) -> Message {
Message::Page(page)
}
}
impl Window {
/// Adds a page to the model we use for the navigation bar.
fn insert_page(&mut self, page: Page) -> segmented_button::SingleSelectEntityMut {
self.nav_bar
.insert()
.text(page.title())
2023-09-13 16:01:04 +02:00
.icon(icon::from_name(page.icon_name()).icon())
.secondary(&mut self.nav_id_to_page, page)
}
fn page_title<Message: 'static>(&self, page: Page) -> Element<Message> {
row!(text(page.title()).size(28), horizontal_space(Length::Fill),).into()
2022-12-21 11:39:49 -07:00
}
fn is_condensed(&self) -> bool {
2022-12-28 12:42:28 +01:00
WINDOW_WIDTH.load(Ordering::Relaxed) < BREAK_POINT
}
fn page(&mut self, page: Page) {
self.nav_bar_toggled_condensed = false;
self.page = page;
}
2023-01-03 21:35:35 +01:00
fn parent_page_button<Message: Clone + From<Page> + 'static>(
&self,
sub_page: impl SubPage,
) -> Element<Message> {
2022-12-21 11:39:49 -07:00
let page = sub_page.parent_page();
column!(
2023-09-13 16:01:04 +02:00
button::icon(icon::from_name("go-previous-symbolic").size(16))
.label(page.title())
.padding(0)
.on_press(Message::from(page)),
2022-12-21 11:39:49 -07:00
row!(
text(sub_page.title()).size(28),
2022-12-21 11:39:49 -07:00
horizontal_space(Length::Fill),
),
)
.spacing(10)
.into()
}
fn set_scale_factor(&mut self, factor: f32) {
self.scale_factor = factor as f64;
self.scale_factor_string = format!("{:.2}", factor);
}
2023-01-03 21:35:35 +01:00
fn sub_page_button<Message: Clone + From<Page> + 'static>(
&self,
sub_page: impl SubPage,
) -> Element<Message> {
2022-12-21 11:39:49 -07:00
iced::widget::Button::new(
2023-09-13 16:39:44 +02:00
list::container(
2022-12-28 12:42:28 +01:00
settings::item_row(vec![
2023-09-13 16:01:04 +02:00
icon::from_name(sub_page.icon_name()).size(20).icon().into(),
2022-12-28 12:42:28 +01:00
column!(
text(sub_page.title()).size(14),
text(sub_page.description()).size(10),
2022-12-28 12:42:28 +01:00
)
.spacing(2)
.into(),
horizontal_space(iced::Length::Fill).into(),
2023-09-13 16:01:04 +02:00
icon::from_name("go-next-symbolic").size(20).icon().into(),
2022-12-28 12:42:28 +01:00
])
.spacing(16),
)
2023-09-13 16:39:44 +02:00
.padding([20, 24]),
2022-12-21 11:39:49 -07:00
)
.padding(0)
2023-09-13 16:01:04 +02:00
.style(theme::iced::Button::Transparent)
.on_press(Message::from(sub_page.into_page()))
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
// .id(BTN.clone())
2022-12-21 11:39:49 -07:00
.into()
}
fn toggle_warning(&mut self) {
self.show_warning = !self.show_warning
}
fn view_unimplemented_page<Message: 'static>(&self, page: Page) -> Element<Message> {
2022-12-21 11:39:49 -07:00
settings::view_column(vec![
self.page_title(page),
text("We haven't created that panel yet, and/or it is using a similar idea as current Pop! designs.").into(),
]).into()
}
2023-01-03 21:35:35 +01:00
fn view_unimplemented_sub_page<'a, Message: Clone + From<Page> + 'static>(
&'a self,
sub_page: impl SubPage,
) -> Element<'a, Message> {
2022-12-21 11:39:49 -07:00
settings::view_column(vec![
self.parent_page_button(sub_page),
text("We haven't created that panel yet, and/or it is using a similar idea as current Pop! designs.").into(),
]).into()
}
}
impl Application for Window {
type Executor = cosmic::executor::Default;
type Flags = ();
type Message = Message;
type Theme = Theme;
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let mut window = Window::default()
.nav_bar_toggled(true)
.show_maximize(true)
.show_minimize(true);
window.title = String::from("COSMIC Design System - Iced");
window.set_scale_factor(1.0);
window.warning_message = String::from("You were not supposed to touch that.");
window.insert_page(Page::Demo);
window.insert_page(Page::Editor);
window.insert_page(Page::WiFi);
window.insert_page(Page::Networking(None));
window.insert_page(Page::Bluetooth);
window.insert_page(Page::Desktop(None)).activate();
window.insert_page(Page::InputDevices(None));
window.insert_page(Page::Displays);
window.insert_page(Page::PowerAndBattery);
window.insert_page(Page::Sound);
window.insert_page(Page::PrintersAndScanners);
window.insert_page(Page::PrivacyAndSecurity);
window.insert_page(Page::SystemAndAccounts(None));
window.insert_page(Page::TimeAndLanguage(None));
window.insert_page(Page::Accessibility);
window.insert_page(Page::Applications);
2023-07-17 11:52:07 -04:00
window.demo.timeline = window.timeline.clone();
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
(window, load_fonts().map(|_| Message::FontsLoaded))
}
fn title(&self) -> String {
self.title.clone()
}
fn subscription(&self) -> Subscription<Message> {
2023-01-05 11:29:14 -07:00
let window_break = subscription::events_with(|event, _| match event {
2022-12-28 12:42:28 +01:00
cosmic::iced::Event::Window(
_window_id,
window::Event::Resized { width, height: _ },
) => {
let old_width = WINDOW_WIDTH.load(Ordering::Relaxed);
if old_width == 0
|| old_width < BREAK_POINT && width > BREAK_POINT
|| old_width > BREAK_POINT && width < BREAK_POINT
{
WINDOW_WIDTH.store(width, Ordering::Relaxed);
Some(())
} else {
None
}
}
_ => None,
2023-01-05 11:29:14 -07:00
});
Subscription::batch(vec![
window_break.map(|_| Message::CondensedViewToggle),
keyboard_nav::subscription().map(Message::KeyboardNav),
2023-07-17 11:52:07 -04:00
self.timeline
.borrow()
.as_subscription()
.map(|(_, instant)| Self::Message::Tick(instant)),
2023-01-05 11:29:14 -07:00
])
}
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
2023-01-05 11:29:14 -07:00
let mut ret = Command::none();
match message {
Message::NavBar(key) => {
if let Some(page) = self.nav_id_to_page.get(key).copied() {
self.nav_bar.activate(key);
self.page(page);
}
}
Message::Page(page) => self.page(page),
Message::Bluetooth(message) => {
self.bluetooth.update(message);
}
2023-01-03 21:35:35 +01:00
Message::Demo(message) => match self.demo.update(message) {
Some(demo::Output::Debug(debug)) => self.debug = debug,
Some(demo::Output::ScalingFactor(factor)) => self.set_scale_factor(factor),
2023-02-27 19:56:53 -05:00
Some(demo::Output::ThemeChanged(theme)) => {
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
self.theme = match theme {
demo::ThemeVariant::Light => Theme::light(),
demo::ThemeVariant::Dark => Theme::dark(),
demo::ThemeVariant::HighContrastDark => Theme::dark_hc(),
demo::ThemeVariant::HighContrastLight => Theme::light_hc(),
demo::ThemeVariant::Custom => Theme::custom(Arc::new(
ThemeBuilder::light()
.bg_color(Srgba::new(1.0, 0.9, 0.9, 1.0))
.text_tint(Rgb::new(0.0, 1.0, 0.0))
.neutral_tint(Rgb::new(0.0, 0.5, 1.0))
.accent(Rgb::new(0.5, 0.1, 0.5))
.success(Rgb::new(0.0, 0.5, 0.3))
.warning(Rgb::new(0.894, 0.816, 0.039))
.destructive(Rgb::new(0.890, 0.145, 0.420))
.build(),
)),
2023-09-13 16:01:04 +02:00
demo::ThemeVariant::System => cosmic::theme::system_preference(),
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
};
2023-02-27 19:56:53 -05:00
}
Some(demo::Output::ToggleWarning) => self.toggle_warning(),
2023-01-03 21:35:35 +01:00
None => (),
},
Message::Editor(message) => self.editor.update(message),
2023-01-03 21:35:35 +01:00
Message::Desktop(message) => match self.desktop.update(message) {
Some(desktop::Output::Page(page)) => self.page(page),
None => (),
},
Message::ToggleNavBar => self.nav_bar_toggled = !self.nav_bar_toggled,
Message::ToggleNavBarCondensed => {
self.nav_bar_toggled_condensed = !self.nav_bar_toggled_condensed
2022-12-28 12:42:28 +01:00
}
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
Message::Drag => return drag(),
Message::Close => return close(),
Message::Minimize => return minimize(true),
Message::Maximize => return toggle_maximize(),
2022-12-28 12:42:28 +01:00
Message::InputChanged => {}
2023-01-05 11:29:14 -07:00
Message::CondensedViewToggle => {}
Message::KeyboardNav(message) => match message {
keyboard_nav::Message::Unfocus => ret = keyboard_nav::unfocus(),
keyboard_nav::Message::FocusNext => ret = widget::focus_next(),
keyboard_nav::Message::FocusPrevious => ret = widget::focus_previous(),
_ => (),
},
Message::ToggleWarning => self.toggle_warning(),
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
Message::FontsLoaded => {}
2023-07-17 11:52:07 -04:00
Message::Tick(instant) => self.timeline.borrow_mut().now(instant),
}
2023-01-05 11:29:14 -07:00
ret
}
fn view(&self) -> Element<Message> {
let (nav_bar_message, nav_bar_toggled) = if self.is_condensed() {
2022-12-28 12:42:28 +01:00
(
Message::ToggleNavBarCondensed,
self.nav_bar_toggled_condensed,
2022-12-28 12:42:28 +01:00
)
} else {
(Message::ToggleNavBar, self.nav_bar_toggled)
2022-12-28 12:42:28 +01:00
};
let mut header = header_bar()
.title("COSMIC Design System - Iced")
.on_close(Message::Close)
.on_drag(Message::Drag)
.start(
nav_bar_toggle()
2023-08-02 11:54:07 +02:00
.on_toggle(nav_bar_message)
.active(nav_bar_toggled),
2022-12-28 12:42:28 +01:00
);
if self.show_maximize {
header = header.on_maximize(Message::Maximize);
}
if self.show_minimize {
header = header.on_minimize(Message::Minimize);
}
let header = Into::<Element<Message>>::into(header).debug(self.debug);
let mut widgets = Vec::with_capacity(2);
if nav_bar_toggled {
let mut nav_bar = nav_bar(&self.nav_bar, Message::NavBar);
2022-12-28 12:42:28 +01:00
if !self.is_condensed() {
nav_bar = nav_bar.max_width(300);
2022-12-28 12:42:28 +01:00
}
let nav_bar: Element<_> = nav_bar.into();
widgets.push(nav_bar.debug(self.debug));
2022-12-28 12:42:28 +01:00
}
if !(self.is_condensed() && nav_bar_toggled) {
2022-12-28 12:42:28 +01:00
let content: Element<_> = match self.page {
Page::Demo => self.demo.view(self).map(Message::Demo),
Page::Editor => self.editor.view(self).map(Message::Editor),
2022-12-28 12:42:28 +01:00
Page::Networking(None) => settings::view_column(vec![
self.page_title(self.page),
column!(
self.sub_page_button(NetworkingPage::Wired),
self.sub_page_button(NetworkingPage::OnlineAccounts),
)
.spacing(16)
.into(),
])
.into(),
2023-01-03 21:35:35 +01:00
Page::Networking(Some(sub_page)) => self.view_unimplemented_sub_page(sub_page),
Page::Bluetooth => self.bluetooth.view(self).map(Message::Bluetooth),
2023-01-03 21:35:35 +01:00
Page::Desktop(desktop_page_opt) => self
.desktop
.view(self, desktop_page_opt)
.map(Message::Desktop),
2022-12-28 12:42:28 +01:00
Page::InputDevices(None) => settings::view_column(vec![
self.page_title(self.page),
column!(
self.sub_page_button(InputDevicesPage::Keyboard),
self.sub_page_button(InputDevicesPage::Touchpad),
self.sub_page_button(InputDevicesPage::Mouse),
)
.spacing(16)
.into(),
])
.into(),
Page::InputDevices(Some(sub_page)) => self.view_unimplemented_sub_page(sub_page),
Page::SystemAndAccounts(None) => settings::view_column(vec![
self.page_title(self.page),
column!(
self.sub_page_button(SystemAndAccountsPage::Users),
self.sub_page_button(SystemAndAccountsPage::About),
self.sub_page_button(SystemAndAccountsPage::Firmware),
)
.spacing(16)
.into(),
])
.into(),
Page::SystemAndAccounts(Some(SystemAndAccountsPage::About)) => {
self.system_and_accounts.view(self)
2022-12-28 12:42:28 +01:00
}
Page::SystemAndAccounts(Some(sub_page)) => {
self.view_unimplemented_sub_page(sub_page)
}
Page::TimeAndLanguage(None) => settings::view_column(vec![
self.page_title(self.page),
column!(
self.sub_page_button(TimeAndLanguagePage::DateAndTime),
self.sub_page_button(TimeAndLanguagePage::RegionAndLanguage),
)
.spacing(16)
.into(),
])
.into(),
Page::TimeAndLanguage(Some(sub_page)) => self.view_unimplemented_sub_page(sub_page),
_ => self.view_unimplemented_page(self.page),
};
widgets.push(
scrollable(row![
horizontal_space(Length::Fill),
content.debug(self.debug),
horizontal_space(Length::Fill),
])
.into(),
);
}
let content = container(row(widgets))
.padding([0, 8, 8, 8])
.width(Length::Fill)
.height(Length::Fill)
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
.style(theme::Container::Background)
2022-12-28 12:42:28 +01:00
.into();
let warning = warning(&self.warning_message)
.on_close(Message::ToggleWarning)
2023-01-16 12:04:39 -07:00
.into();
if self.show_warning {
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
column![
header,
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
container(column(vec![
warning,
iced::widget::vertical_space(Length::Fixed(12.0)).into(),
content,
]))
.style(theme::Container::Background)
]
.into()
2023-01-16 12:04:39 -07:00
} else {
column(vec![header, content]).into()
}
}
fn scale_factor(&self) -> f64 {
self.scale_factor
}
fn theme(&self) -> Theme {
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
self.theme.clone()
}
}