Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Un-bitrot cosmic example #677

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions examples/cosmic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 System76 <[email protected]>
// SPDX-License-Identifier: MPL-2.0

use cosmic::iced::{Application, Settings};
use cosmic::{app::Settings, iced::Limits};

mod window;
use env_logger::Env;
Expand All @@ -15,13 +15,9 @@ pub fn main() -> cosmic::iced::Result {
env_logger::init_from_env(env);
cosmic::icon_theme::set_default("Pop");
#[allow(clippy::field_reassign_with_default)]
let settings = Settings {
default_font: cosmic::font::default(),
window: cosmic::iced::window::Settings {
min_size: Some(cosmic::iced::Size::new(600., 300.)),
..cosmic::iced::window::Settings::default()
},
..Settings::default()
};
Window::run(settings)
let settings = Settings::default()
.default_font(cosmic::font::default())
.size_limits(Limits::NONE.min_width(600.).min_height(300.));
cosmic::app::run::<window::Window>(settings, ())?;
Ok(())
}
109 changes: 62 additions & 47 deletions examples/cosmic/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/// Copyright 2022 System76 <[email protected]>
// SPDX-License-Identifier: MPL-2.0
use cosmic::{
app::{command, Application, Core},
cosmic_theme::{
palette::{rgb::Rgb, Srgba},
ThemeBuilder,
},
font::load_fonts,
iced::{self, Application, Length, Subscription, Task},
iced::{
subscription,
self,
widget::{self, column, container, horizontal_space, row, text},
window::{self, close, drag, minimize, toggle_maximize},
Length, Size, Subscription,
},
iced_futures::event::listen_raw,
keyboard_nav,
Expand All @@ -20,7 +20,7 @@ use cosmic::{
button, header_bar, icon, list, nav_bar, nav_bar_toggle, scrollable, segmented_button,
settings, warning,
},
Element,
ApplicationExt, Element,
};
use cosmic_time::{Instant, Timeline};
use std::{
Expand Down Expand Up @@ -145,8 +145,8 @@ impl Default for Page {
static WINDOW_WIDTH: AtomicU32 = AtomicU32::new(0);
const BREAK_POINT: u32 = 900;

#[derive(Default)]
pub struct Window {
core: Core,
bluetooth: bluetooth::State,
debug: bool,
demo: demo::State,
Expand All @@ -161,7 +161,6 @@ pub struct Window {
show_minimize: bool,
system_and_accounts: system_and_accounts::State,
theme: Theme,
title: String,
show_warning: bool,
warning_message: String,
scale_factor: f64,
Expand Down Expand Up @@ -210,7 +209,6 @@ pub enum Message {
ToggleNavBar,
ToggleNavBarCondensed,
ToggleWarning,
FontsLoaded,
Tick(Instant),
}

Expand All @@ -231,7 +229,7 @@ impl Window {
}

fn page_title<Message: 'static>(&self, page: Page) -> Element<Message> {
row!(text(page.title()).size(28), horizontal_space(Length::Fill),).into()
row!(text(page.title()).size(28), horizontal_space(),).into()
}

fn is_condensed(&self) -> bool {
Expand All @@ -253,10 +251,7 @@ impl Window {
.label(page.title())
.padding(0)
.on_press(Message::from(page)),
row!(
text(sub_page.title()).size(28),
horizontal_space(Length::Fill),
),
row!(text(sub_page.title()).size(28), horizontal_space(),),
)
.spacing(10)
.into()
Expand All @@ -281,7 +276,7 @@ impl Window {
)
.spacing(2)
.into(),
horizontal_space(iced::Length::Fill).into(),
horizontal_space().into(),
icon::from_name("go-next-symbolic").size(20).icon().into(),
])
.spacing(16),
Expand All @@ -290,7 +285,7 @@ impl Window {
)
.width(Length::Fill)
.padding(0)
.style(theme::iced::Button::Transparent)
.class(theme::iced::Button::Transparent)
.on_press(Message::from(sub_page.into_page()))
// .id(BTN.clone())
.into()
Expand Down Expand Up @@ -322,17 +317,43 @@ impl Application for Window {
type Executor = cosmic::executor::Default;
type Flags = ();
type Message = Message;
type Theme = Theme;

fn new(_flags: ()) -> (Self, Task<Self::Message>) {
let mut window = Window::default()
.nav_bar_toggled(true)
.show_maximize(true)
.show_minimize(true);
/// The unique application ID to supply to the window manager.
const APP_ID: &'static str = "org.cosmic.CosmicDemo";

fn core(&self) -> &Core {
&self.core
}

fn core_mut(&mut self) -> &mut Core {
&mut self.core
}

fn init(core: Core, _flags: ()) -> (Self, command::Task<Self::Message>) {
let mut window = Window {
core,
bluetooth: bluetooth::State::default(),
debug: false,
demo: demo::State::default(),
editor: editor::State::default(),
desktop: desktop::State::default(),
nav_bar: segmented_button::SingleSelectModel::default(),
nav_id_to_page: segmented_button::SecondaryMap::default(),
nav_bar_toggled_condensed: false,
nav_bar_toggled: true,
page: Page::default(),
show_maximize: true,
show_minimize: true,
system_and_accounts: system_and_accounts::State::default(),
theme: Theme::default(),
show_warning: false,
warning_message: String::from("You were not supposed to touch that."),
scale_factor: 1.0,
scale_factor_string: String::new(),
timeline: Rc::new(RefCell::new(Timeline::default())),
};

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);
Expand All @@ -352,19 +373,18 @@ impl Application for Window {
window.insert_page(Page::Applications);
window.demo.timeline = window.timeline.clone();

(window, load_fonts().map(|_| Message::FontsLoaded))
}
let command = window.set_window_title(
"COSMIC Design System - Iced".into(),
window.core.main_window_id().unwrap(),
);

fn title(&self) -> String {
self.title.clone()
(window, command)
}

fn subscription(&self) -> Subscription<Message> {
let window_break = listen_raw(|event, _| match event {
cosmic::iced::Event::Window(
_window_id,
window::Event::Resized { width, height: _ },
) => {
let window_break = listen_raw(|event, _, _| match event {
cosmic::iced::Event::Window(window::Event::Resized(Size { width, height: _ })) => {
let width = width as u32;
let old_width = WINDOW_WIDTH.load(Ordering::Relaxed);
if old_width == 0
|| old_width < BREAK_POINT && width > BREAK_POINT
Expand All @@ -385,12 +405,12 @@ impl Application for Window {
self.timeline
.borrow()
.as_subscription()
.map(|(_, instant)| Self::Message::Tick(instant)),
.map(|(_, instant)| Message::Tick(instant)),
])
}

fn update(&mut self, message: Message) -> iced::Task<Self::Message> {
let mut ret = Task::none();
fn update(&mut self, message: Message) -> command::Task<Self::Message> {
let mut ret = command::Task::none();
match message {
Message::NavBar(key) => {
if let Some(page) = self.nav_id_to_page.get(key).copied() {
Expand All @@ -404,7 +424,10 @@ impl Application for Window {
}
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),
Some(demo::Output::ScalingFactor(factor)) => {
self.set_scale_factor(factor);
ret = self.core.set_scaling_factor(factor);
}
Some(demo::Output::ThemeChanged(theme)) => {
self.theme = match theme {
demo::ThemeVariant::Light => Theme::light(),
Expand All @@ -424,6 +447,7 @@ impl Application for Window {
)),
demo::ThemeVariant::System => cosmic::theme::system_preference(),
};
ret = command::set_theme(self.theme.clone())
}
Some(demo::Output::ToggleWarning) => self.toggle_warning(),
None => (),
Expand Down Expand Up @@ -451,7 +475,6 @@ impl Application for Window {
_ => (),
},
Message::ToggleWarning => self.toggle_warning(),
Message::FontsLoaded => {} // Message::Tick(instant) => self.timeline.borrow_mut().now(instant), Message::Tick(instant) => self.timeline.borrow_mut().now(instant),
Message::Tick(instant) => self.timeline.borrow_mut().now(instant),
}
ret
Expand Down Expand Up @@ -577,7 +600,7 @@ impl Application for Window {
.padding([0, 8, 8, 8])
.width(Length::Fill)
.height(Length::Fill)
.style(theme::Container::Background)
.class(theme::Container::Background)
.into();
let warning = warning(&self.warning_message)
.on_close(Message::ToggleWarning)
Expand All @@ -587,22 +610,14 @@ impl Application for Window {
header,
container(column(vec![
warning,
iced::widget::vertical_space(Length::Fixed(12.0)).into(),
iced::widget::Space::with_height(Length::Fixed(12.0)).into(),
content,
]))
.style(theme::Container::Background)
.class(theme::Container::Background)
]
.into()
} else {
column(vec![header, content]).into()
}
}

fn scale_factor(&self) -> f64 {
self.scale_factor
}

fn theme(&self) -> Theme {
self.theme.clone()
}
}
5 changes: 3 additions & 2 deletions examples/cosmic/src/window/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ impl State {
column!(
list_column().add(settings::item(
"Bluetooth",
toggler(None, self.enabled, Message::Enable)
toggler(self.enabled).on_toggle(Message::Enable)
)),
text("Now visible as \"TODO\", just kidding")
)
.spacing(8)
.into(),
settings::view_section("Devices")
settings::section()
.title("Devices")
.add(settings::item("No devices found", text("")))
.into(),
])
Expand Down
40 changes: 21 additions & 19 deletions examples/cosmic/src/window/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ impl Default for State {
checkbox_value: false,
dropdown_selected: Some(0),
dropdown_options: vec!["Option 1", "Option 2", "Option 3", "Option 4"],
scaling_value: spin_button::Model::default()
.value(1.0)
.min(0.5)
.max(2.0)
.step(0.25),
scaling_value: spin_button::Model {
value: 1.0.into(),
min: 0.5.into(),
max: 2.0.into(),
step: 0.25.into(),
},
slider_value: 50.0,
spin_button: spin_button::Model::default().min(-10).max(10),
toggler_value: false,
Expand Down Expand Up @@ -232,7 +233,7 @@ impl State {
]
.into_iter()
.fold(
column![].spacing(10).align_items(Alignment::Center),
column![].spacing(10).align_x(Alignment::Center),
|row, theme| {
row.push(radio(
format!("{:?}", theme),
Expand All @@ -258,12 +259,13 @@ impl State {
match self.tab_bar.active_data() {
None => panic!("no tab is active"),
Some(DemoView::TabA) => settings::view_column(vec![
settings::view_section("Debug")
settings::section()
.title("Debug")
.add(settings::item("Debug theme", choose_theme))
.add(settings::item("Debug icon theme", choose_icon_theme))
.add(settings::item(
"Debug layout",
toggler(None, window.debug, Message::Debug),
toggler(window.debug).on_toggle(Message::Debug),
))
.add(settings::item(
"Scaling Factor",
Expand All @@ -276,10 +278,11 @@ impl State {
.into(),
]))
.into(),
settings::view_section("Controls")
settings::section()
.title("Controls")
.add(settings::item(
"Toggler",
toggler(None, self.toggler_value, Message::TogglerToggled),
toggler(self.toggler_value).on_toggle(Message::TogglerToggled),
))
.add(settings::item(
"Pick List (TODO)",
Expand All @@ -305,8 +308,8 @@ impl State {
.add(settings::item_row(vec![checkbox(
"Checkbox",
self.checkbox_value,
Message::CheckboxToggled,
)
.on_toggle(Message::CheckboxToggled)
.into()]))
.add(settings::item(
format!(
Expand Down Expand Up @@ -354,7 +357,7 @@ impl State {
.width(Length::Shrink)
.on_activate(Message::MultiSelection)
.apply(container)
.center_x()
.center_x(Length::Fill)
.width(Length::Fill)
.into(),
text("Vertical With Spacing").into(),
Expand Down Expand Up @@ -424,13 +427,12 @@ impl State {
])
.padding(0)
.into(),
Some(DemoView::TabC) => {
settings::view_column(vec![settings::view_section("Tab C")
.add(text("Nothing here yet").width(Length::Fill))
.into()])
.padding(0)
.into()
}
Some(DemoView::TabC) => settings::view_column(vec![settings::section()
.title("Tab C")
.add(text("Nothing here yet").width(Length::Fill))
.into()])
.padding(0)
.into(),
},
container(text("Background container with some text").size(24))
.layer(cosmic_theme::Layer::Background)
Expand Down
Loading