2025-03-05 15:41:14 -05:00
|
|
|
use cosmic::app::{Core, Task};
|
2025-02-24 23:13:01 -05:00
|
|
|
use cosmic::cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity;
|
2025-02-21 14:29:12 -05:00
|
|
|
use cosmic::iced::event::listen_with;
|
2023-09-17 17:35:50 -07:00
|
|
|
use cosmic::iced::window::Id;
|
2025-03-05 15:41:14 -05:00
|
|
|
use cosmic::iced::{self, Length, Limits};
|
2023-09-17 17:35:50 -07:00
|
|
|
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-03-05 15:41:14 -05:00
|
|
|
use cosmic::surface::action::{app_popup, destroy_popup};
|
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;
|
2023-09-17 17:35:50 -07:00
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
|
2023-09-17 17:35:50 -07:00
|
|
|
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,
|
2023-09-17 17:35:50 -07:00
|
|
|
}
|
|
|
|
|
|
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)]
|
2023-09-17 17:35:50 -07:00
|
|
|
pub enum Message {
|
|
|
|
|
PopupClosed(Id),
|
2025-01-22 16:31:51 -05:00
|
|
|
PopupCloseRequested(Id),
|
2023-09-17 17:35:50 -07:00
|
|
|
ToggleExampleRow(bool),
|
2025-01-22 16:31:51 -05:00
|
|
|
Selected(usize),
|
2025-03-05 15:41:14 -05:00
|
|
|
Surface(cosmic::surface::Action),
|
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
|
|
|
}
|
|
|
|
|
|
2025-03-05 15:41:14 -05:00
|
|
|
// impl From<Message> for MessageWrapper<Message> {
|
|
|
|
|
// fn from(value: Message) -> Self {
|
|
|
|
|
// match value {
|
|
|
|
|
// Message::Surface(s) => MessageWrapper::Surface(s),
|
|
|
|
|
// m => MessageWrapper::Message(m),
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// impl From<SurfaceMessage> for Message {
|
|
|
|
|
// fn from(value: SurfaceMessage) -> Self {
|
|
|
|
|
// Message::Surface(value)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-09-17 17:35:50 -07:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 15:41:14 -05:00
|
|
|
fn init(core: Core, _flags: Self::Flags) -> (Self, Task<Message>) {
|
2023-09-17 17:35:50 -07:00
|
|
|
let window = Window {
|
|
|
|
|
core,
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
2024-10-16 20:36:46 -04:00
|
|
|
(window, Task::none())
|
2023-09-17 17:35:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_close_requested(&self, id: window::Id) -> Option<Message> {
|
|
|
|
|
Some(Message::PopupClosed(id))
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 15:41:14 -05:00
|
|
|
fn update(&mut self, message: Message) -> Task<Message> {
|
2023-09-17 17:35:50 -07:00
|
|
|
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
|
|
|
|
2025-03-05 15:41:14 -05:00
|
|
|
Message::Surface(a) => {
|
2025-03-05 15:45:44 -05:00
|
|
|
return cosmic::task::message(cosmic::Action::Cosmic(
|
|
|
|
|
cosmic::app::Action::Surface(a),
|
|
|
|
|
));
|
2025-03-05 15:41:14 -05:00
|
|
|
}
|
2025-01-22 16:31:51 -05:00
|
|
|
Message::Selected(i) => {
|
|
|
|
|
self.selected = Some(i);
|
2025-03-05 15:41:14 -05:00
|
|
|
return cosmic::task::message(cosmic::Action::Cosmic(
|
|
|
|
|
cosmic::app::Action::Surface(cosmic::surface::action::destroy_popup(
|
|
|
|
|
self.dropdown_id,
|
|
|
|
|
)),
|
2025-01-22 16:31:51 -05:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
Message::OpenDropdown(sctk_popup_settings, view) => {
|
|
|
|
|
self.dropdown_id = sctk_popup_settings.id;
|
2025-03-05 15:41:14 -05:00
|
|
|
return cosmic::task::message(cosmic::Action::Cosmic(
|
|
|
|
|
cosmic::app::Action::Surface(cosmic::surface::action::app_popup(
|
|
|
|
|
move |_: &mut Window| sctk_popup_settings.clone(),
|
|
|
|
|
Some(Box::new(move |_: &Window| view().map(cosmic::Action::App))),
|
2025-02-21 14:29:12 -05:00
|
|
|
)),
|
|
|
|
|
));
|
2025-01-22 16:31:51 -05:00
|
|
|
}
|
|
|
|
|
Message::PopupCloseRequested(id) => {
|
2025-03-05 15:41:14 -05:00
|
|
|
return cosmic::task::message(cosmic::Action::Cosmic(
|
|
|
|
|
cosmic::app::Action::Surface(cosmic::surface::action::destroy_popup(id)),
|
|
|
|
|
));
|
2025-01-22 16:31:51 -05:00
|
|
|
}
|
2025-03-05 15:41:14 -05:00
|
|
|
};
|
2024-12-31 17:18:49 -05:00
|
|
|
Task::none()
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 15:41:14 -05:00
|
|
|
fn view(&self) -> Element<Message> {
|
2025-02-26 14:14:15 -05:00
|
|
|
let btn = self.core.applet.icon_button("display-symbolic").on_press(
|
|
|
|
|
if let Some(id) = self.popup {
|
2025-03-05 15:41:14 -05:00
|
|
|
Message::Surface(destroy_popup(id))
|
2025-02-26 14:14:15 -05:00
|
|
|
} else {
|
2025-03-05 15:41:14 -05:00
|
|
|
Message::Surface(app_popup::<Window>(
|
2025-02-26 14:14:15 -05:00
|
|
|
|state: &mut Window| {
|
|
|
|
|
let new_id = Id::unique();
|
|
|
|
|
state.popup = Some(new_id);
|
2025-03-05 15:41:14 -05:00
|
|
|
let popup_settings = state.core.applet.get_popup_settings(
|
2025-02-26 14:14:15 -05:00
|
|
|
state.core.main_window_id().unwrap(),
|
|
|
|
|
new_id,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
);
|
2025-03-05 15:41:14 -05:00
|
|
|
|
2025-02-26 14:14:15 -05:00
|
|
|
popup_settings
|
2024-12-31 10:57:08 -05:00
|
|
|
},
|
2025-03-05 15:41:14 -05:00
|
|
|
Some(Box::new(move |state: &Window| {
|
|
|
|
|
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.)),
|
|
|
|
|
))
|
|
|
|
|
.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),
|
|
|
|
|
);
|
|
|
|
|
Element::from(state.core.applet.popup_container(content_list))
|
|
|
|
|
.map(cosmic::Action::App)
|
|
|
|
|
})),
|
|
|
|
|
))
|
2024-12-31 17:18:49 -05:00
|
|
|
},
|
2025-02-26 14:14:15 -05:00
|
|
|
);
|
|
|
|
|
|
2025-03-05 15:45:44 -05:00
|
|
|
Element::from(self.core.applet.applet_tooltip::<Message>(
|
|
|
|
|
btn,
|
|
|
|
|
"test",
|
|
|
|
|
self.popup.is_some(),
|
|
|
|
|
|a| Message::Surface(a),
|
|
|
|
|
))
|
2023-09-17 17:35:50 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-05 15:41:14 -05:00
|
|
|
fn view_window(&self, _id: Id) -> Element<Message> {
|
2024-12-27 16:17:16 -05:00
|
|
|
"oops".into()
|
2023-09-17 17:35:50 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
fn style(&self) -> Option<cosmic::iced_runtime::Appearance> {
|
2023-09-18 07:45:11 +02:00
|
|
|
Some(cosmic::applet::style())
|
2023-09-17 17:35:50 -07:00
|
|
|
}
|
|
|
|
|
}
|