libcosmic/examples/applet/src/window.rs

236 lines
8.8 KiB
Rust
Raw Normal View History

2024-12-31 17:18:49 -05:00
use cosmic::app::message::{SurfaceMessage, SurfaceMessageHandler};
use cosmic::app::Core;
use cosmic::iced::window::Id;
2024-10-16 20:36:46 -04:00
use cosmic::iced::{Length, Limits, Task};
use cosmic::iced_runtime::core::window;
2025-01-22 16:31:51 -05:00
use cosmic::iced_runtime::platform_specific::wayland::popup::SctkPopupSettings;
2024-12-31 10:57:08 -05:00
use cosmic::iced_runtime::platform_specific::wayland::subsurface;
2025-01-22 16:31:51 -05:00
use cosmic::widget::dropdown::DropdownView;
use cosmic::widget::{autosize, dropdown, layer_container, list_column, settings, toggler};
use cosmic::{iced_core, Element};
use once_cell::sync::Lazy;
const ID: &str = "com.system76.CosmicAppletExample";
2025-01-22 16:31:51 -05:00
static SUBSURFACE_ID: Lazy<cosmic::widget::Id> =
Lazy::new(|| cosmic::widget::Id::new("subsurface"));
pub struct Window {
core: Core,
popup: Option<Id>,
2024-12-31 10:57:08 -05:00
example_row: bool,
2025-01-22 16:31:51 -05:00
selected: Option<usize>,
subsurface_id: Id,
dropdown_id: Id,
}
2025-01-22 16:31:51 -05:00
impl Default for Window {
fn default() -> Self {
Self {
core: Core::default(),
popup: None,
example_row: false,
selected: None,
subsurface_id: Id::unique(),
dropdown_id: Id::unique(),
}
}
}
#[derive(Clone)]
pub enum Message {
PopupClosed(Id),
2025-01-22 16:31:51 -05:00
PopupCloseRequested(Id),
ToggleExampleRow(bool),
2025-01-22 16:31:51 -05:00
Selected(usize),
2024-12-31 17:18:49 -05:00
Surface(SurfaceMessage),
2025-01-22 16:31:51 -05:00
OpenDropdown(SctkPopupSettings, DropdownView<Message>),
}
impl std::fmt::Debug for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::PopupClosed(arg0) => f.debug_tuple("PopupClosed").field(arg0).finish(),
Self::PopupCloseRequested(arg0) => {
f.debug_tuple("PopupCloseRequested").field(arg0).finish()
}
Self::ToggleExampleRow(arg0) => f.debug_tuple("ToggleExampleRow").field(arg0).finish(),
Self::Selected(arg0) => f.debug_tuple("Selected").field(arg0).finish(),
Self::Surface(arg0) => f.debug_tuple("Surface").field(arg0).finish(),
Self::OpenDropdown(arg0, _) => f.debug_tuple("OpenDropdown").field(arg0).finish(),
}
}
2024-12-31 17:18:49 -05:00
}
impl SurfaceMessageHandler for Message {
fn to_surface_message(self) -> cosmic::app::message::MessageWrapper<Self> {
match self {
Message::Surface(msg) => cosmic::app::message::MessageWrapper::Surface(msg),
msg => cosmic::app::message::MessageWrapper::Message(msg),
}
}
}
impl From<SurfaceMessage> for Message {
fn from(value: SurfaceMessage) -> Self {
Message::Surface(value)
}
}
impl cosmic::Application for Window {
type Executor = cosmic::SingleThreadExecutor;
type Flags = ();
type Message = Message;
const APP_ID: &'static str = ID;
fn core(&self) -> &Core {
&self.core
}
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
2024-10-16 20:36:46 -04:00
fn init(core: Core, _flags: Self::Flags) -> (Self, Task<cosmic::app::Message<Self::Message>>) {
let window = Window {
core,
..Default::default()
};
2024-10-16 20:36:46 -04:00
(window, Task::none())
}
fn on_close_requested(&self, id: window::Id) -> Option<Message> {
Some(Message::PopupClosed(id))
}
2024-10-16 20:36:46 -04:00
fn update(&mut self, message: Self::Message) -> Task<cosmic::app::Message<Self::Message>> {
match message {
Message::PopupClosed(id) => {
if self.popup.as_ref() == Some(&id) {
self.popup = None;
}
}
2024-12-27 16:17:16 -05:00
Message::ToggleExampleRow(toggled) => {
2024-12-31 10:57:08 -05:00
self.example_row = toggled;
}
2024-12-31 17:18:49 -05:00
Message::Surface(surface_message) => {}
2025-01-22 16:31:51 -05:00
Message::Selected(i) => {
self.selected = Some(i);
return cosmic::task::message(cosmic::app::message::destroy_popup::<Window>(
self.dropdown_id,
));
}
Message::OpenDropdown(sctk_popup_settings, view) => {
self.dropdown_id = sctk_popup_settings.id;
return cosmic::task::message(cosmic::app::message::get_popup::<
Window,
>(
move |_: &mut Window| sctk_popup_settings.clone(),
Some(
move |_: &Window| -> cosmic::Element<'static, cosmic::app::Message<Message>> {
view().map(cosmic::app::Message::App)
},
),
));
}
Message::PopupCloseRequested(id) => {
dbg!(id, self.dropdown_id);
return cosmic::task::message(cosmic::app::message::destroy_popup::<Window>(id));
}
2024-12-31 17:18:49 -05:00
}
Task::none()
}
fn view(&self) -> Element<Self::Message> {
cosmic::widget::wayland::tooltip::widget::Tooltip::new(
self.core.applet.icon_button("display-symbolic").on_press(
if let Some(id) = self.popup {
cosmic::app::message::destroy_popup::<Window>(id)
} else {
cosmic::app::message::get_popup::<Window>(
|state: &mut Window| {
let new_id = Id::unique();
state.popup = Some(new_id);
let mut popup_settings = state.core.applet.get_popup_settings(
state.core.main_window_id().unwrap(),
new_id,
None,
None,
None,
);
popup_settings.positioner.size_limits = Limits::NONE
.max_width(372.0)
.min_width(300.0)
.min_height(200.0)
.max_height(1080.0)
.height(500)
.width(500);
popup_settings.positioner.size = Some((500, 500));
popup_settings
},
Some(
move |state: &Window| -> cosmic::Element<
'static,
cosmic::app::Message<Message>,
> {
{
let content_list = list_column().padding(5).spacing(0).add(
settings::item(
"Example row",
cosmic::widget::container(
toggler(state.example_row).on_toggle(|value| {
Message::ToggleExampleRow(value)
}),
)
.height(Length::Fixed(50.)),
),
2025-01-22 16:31:51 -05:00
).add(dropdown(&["1", "asdf", "hello", "test"], state.selected, Message::Selected).with_popup(state.popup.unwrap_or(Id::NONE), Message::OpenDropdown).on_close_popup(Message::PopupCloseRequested)
);
2024-12-31 17:18:49 -05:00
Element::from(
state.core.applet.popup_container(content_list),
)
.map(cosmic::app::Message::App)
}
},
),
)
},
),
2025-01-22 16:31:51 -05:00
|_| {
cosmic::app::message::get_subsurface(
2024-12-31 10:57:08 -05:00
|app: &mut Window| {
subsurface::SctkSubsurfaceSettings {
parent: window::Id::RESERVED,
2025-01-22 16:31:51 -05:00
id: app.subsurface_id,
2024-12-31 10:57:08 -05:00
loc: iced_core::Point { x: -100., y: 0. },
2025-01-22 16:31:51 -05:00
size: Some((10., 10.).into()),
2024-12-31 10:57:08 -05:00
z: 1,
}
},
2025-01-22 16:31:51 -05:00
Some(|_: &Window| {
autosize::autosize(
layer_container(cosmic::widget::text("hello"))
.layer(cosmic::cosmic_theme::Layer::Background),
SUBSURFACE_ID.clone(),
)
.into()
}),
2024-12-31 17:18:49 -05:00
)
},
cosmic::app::message::destroy_subsurface::<Window>(
2025-01-22 16:31:51 -05:00
self.subsurface_id
2024-12-31 17:18:49 -05:00
),
2024-12-31 10:57:08 -05:00
)
.into()
}
fn view_window(&self, _id: Id) -> Element<Self::Message> {
2024-12-27 16:17:16 -05:00
"oops".into()
}
2024-10-16 20:36:46 -04:00
fn style(&self) -> Option<cosmic::iced_runtime::Appearance> {
Some(cosmic::applet::style())
}
}