feat: popup container helper
This commit is contained in:
parent
49f0f34270
commit
00eed9c741
5 changed files with 112 additions and 51 deletions
|
|
@ -7,7 +7,7 @@ edition = "2021"
|
||||||
name = "cosmic"
|
name = "cosmic"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["iced_winit", "wgpu"]
|
default = ["wayland"]
|
||||||
debug = ["iced/debug"]
|
debug = ["iced/debug"]
|
||||||
wayland = ["iced/wayland"]
|
wayland = ["iced/wayland"]
|
||||||
wgpu = ["iced/wgpu"]
|
wgpu = ["iced/wgpu"]
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ mod window;
|
||||||
|
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
iced::{sctk_settings::InitialSurface, Application},
|
iced::{sctk_settings::InitialSurface, Application},
|
||||||
iced_native::window::Settings,
|
|
||||||
iced_native::command::platform_specific::wayland::window::SctkWindowSettings,
|
iced_native::command::platform_specific::wayland::window::SctkWindowSettings,
|
||||||
|
iced_native::window::Settings,
|
||||||
settings,
|
settings,
|
||||||
};
|
};
|
||||||
use cosmic_panel_config::PanelSize;
|
use cosmic_panel_config::PanelSize;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
use crate::dbus::{self, PowerDaemonProxy};
|
use crate::dbus::{self, PowerDaemonProxy};
|
||||||
use crate::graphics::{get_current_graphics, set_graphics, Graphics};
|
use crate::graphics::{get_current_graphics, set_graphics, Graphics};
|
||||||
use cosmic::applet::{get_popup_settings, icon_button};
|
use cosmic::applet::{get_popup_settings, icon_button};
|
||||||
|
use cosmic::iced_style::application::{self, Appearance};
|
||||||
use cosmic::separator;
|
use cosmic::separator;
|
||||||
|
use cosmic::theme::Container;
|
||||||
|
use cosmic::widget::widget::container;
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
iced::widget::{column, radio, text},
|
iced::widget::{column, radio, text},
|
||||||
iced::{self, Application, Command, Length},
|
iced::{self, Application, Command, Length},
|
||||||
|
|
@ -10,17 +13,23 @@ use cosmic::{
|
||||||
Element,
|
Element,
|
||||||
};
|
};
|
||||||
use cosmic_panel_config::{PanelAnchor, PanelSize};
|
use cosmic_panel_config::{PanelAnchor, PanelSize};
|
||||||
use iced_sctk::alignment::Horizontal;
|
use iced_sctk::Color;
|
||||||
|
use iced_sctk::alignment::{Horizontal, Vertical};
|
||||||
use iced_sctk::commands::popup::{destroy_popup, get_popup};
|
use iced_sctk::commands::popup::{destroy_popup, get_popup};
|
||||||
use zbus::Connection;
|
use zbus::Connection;
|
||||||
|
|
||||||
#[derive(Default, Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum State {
|
enum State {
|
||||||
#[default]
|
SelectGraphicsMode(bool),
|
||||||
SelectGraphicsMode,
|
|
||||||
SettingGraphicsMode(Graphics),
|
SettingGraphicsMode(Graphics),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for State {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::SelectGraphicsMode(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
popup: Option<window::Id>,
|
popup: Option<window::Id>,
|
||||||
|
|
@ -83,7 +92,6 @@ impl Application for Window {
|
||||||
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
|
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::SelectGraphicsMode(new_graphics_mode) => {
|
Message::SelectGraphicsMode(new_graphics_mode) => {
|
||||||
dbg!(new_graphics_mode);
|
|
||||||
if let Some((_, proxy)) = self.dbus.as_ref() {
|
if let Some((_, proxy)) = self.dbus.as_ref() {
|
||||||
self.state = State::SettingGraphicsMode(new_graphics_mode);
|
self.state = State::SettingGraphicsMode(new_graphics_mode);
|
||||||
return Command::perform(
|
return Command::perform(
|
||||||
|
|
@ -96,9 +104,8 @@ impl Application for Window {
|
||||||
}
|
}
|
||||||
Message::AppliedGraphicsMode(g) => {
|
Message::AppliedGraphicsMode(g) => {
|
||||||
if let Some(g) = g {
|
if let Some(g) = g {
|
||||||
dbg!(g);
|
|
||||||
self.graphics_mode.replace(g);
|
self.graphics_mode.replace(g);
|
||||||
self.state = State::SelectGraphicsMode;
|
self.state = State::SelectGraphicsMode(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::TogglePopup => {
|
Message::TogglePopup => {
|
||||||
|
|
@ -115,7 +122,9 @@ impl Application for Window {
|
||||||
|cur_graphics| Message::CurrentGraphics(cur_graphics.ok()),
|
|cur_graphics| Message::CurrentGraphics(cur_graphics.ok()),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
let popup_settings = get_popup_settings(window::Id::new(0), new_id, None, None);
|
let mut popup_settings =
|
||||||
|
get_popup_settings(window::Id::new(0), new_id, None, None);
|
||||||
|
popup_settings.positioner.size = (200, 240);
|
||||||
commands.push(get_popup(popup_settings));
|
commands.push(get_popup(popup_settings));
|
||||||
return Command::batch(commands);
|
return Command::batch(commands);
|
||||||
}
|
}
|
||||||
|
|
@ -136,7 +145,6 @@ impl Application for Window {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Message::CurrentGraphics(g) => {
|
Message::CurrentGraphics(g) => {
|
||||||
dbg!(g);
|
|
||||||
if let Some(g) = g {
|
if let Some(g) = g {
|
||||||
self.graphics_mode.replace(g);
|
self.graphics_mode.replace(g);
|
||||||
}
|
}
|
||||||
|
|
@ -147,39 +155,49 @@ impl Application for Window {
|
||||||
|
|
||||||
fn view_popup(&self, _: window::Id) -> Element<Message> {
|
fn view_popup(&self, _: window::Id) -> Element<Message> {
|
||||||
let content = match self.state {
|
let content = match self.state {
|
||||||
State::SelectGraphicsMode => column(vec![
|
State::SelectGraphicsMode(pending_restart) => {
|
||||||
radio(
|
let mut content_list = vec![
|
||||||
"Integrated Graphics",
|
radio(
|
||||||
Graphics::Integrated,
|
"Integrated Graphics",
|
||||||
self.graphics_mode,
|
Graphics::Integrated,
|
||||||
|g| Message::SelectGraphicsMode(g),
|
self.graphics_mode,
|
||||||
)
|
|g| Message::SelectGraphicsMode(g),
|
||||||
.into(),
|
)
|
||||||
radio(
|
.into(),
|
||||||
"Nvidia Graphics",
|
radio(
|
||||||
Graphics::Nvidia,
|
"Nvidia Graphics",
|
||||||
self.graphics_mode,
|
Graphics::Nvidia,
|
||||||
|g| Message::SelectGraphicsMode(g),
|
self.graphics_mode,
|
||||||
)
|
|g| Message::SelectGraphicsMode(g),
|
||||||
.into(),
|
)
|
||||||
radio(
|
.into(),
|
||||||
"Hybrid Graphics",
|
radio(
|
||||||
Graphics::Hybrid,
|
"Hybrid Graphics",
|
||||||
self.graphics_mode,
|
Graphics::Hybrid,
|
||||||
|g| Message::SelectGraphicsMode(g),
|
self.graphics_mode,
|
||||||
)
|
|g| Message::SelectGraphicsMode(g),
|
||||||
.into(),
|
)
|
||||||
radio(
|
.into(),
|
||||||
"Compute Graphics",
|
radio(
|
||||||
Graphics::Compute,
|
"Compute Graphics",
|
||||||
self.graphics_mode,
|
Graphics::Compute,
|
||||||
|g| Message::SelectGraphicsMode(g),
|
self.graphics_mode,
|
||||||
)
|
|g| Message::SelectGraphicsMode(g),
|
||||||
.into(),
|
)
|
||||||
])
|
.into(),
|
||||||
.padding([8, 0])
|
];
|
||||||
.spacing(8)
|
if pending_restart {
|
||||||
.into(),
|
content_list.insert(
|
||||||
|
0,
|
||||||
|
text("Restart to apply changes")
|
||||||
|
.width(Length::Fill)
|
||||||
|
.horizontal_alignment(Horizontal::Center)
|
||||||
|
.size(16)
|
||||||
|
.into(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
column(content_list).padding([8, 0]).spacing(8).into()
|
||||||
|
}
|
||||||
State::SettingGraphicsMode(graphics) => {
|
State::SettingGraphicsMode(graphics) => {
|
||||||
let graphics_str = match graphics {
|
let graphics_str = match graphics {
|
||||||
Graphics::Integrated => "integrated",
|
Graphics::Integrated => "integrated",
|
||||||
|
|
@ -196,7 +214,7 @@ impl Application for Window {
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
column(vec![
|
container(column(vec![
|
||||||
text("Graphics Mode")
|
text("Graphics Mode")
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.horizontal_alignment(Horizontal::Center)
|
.horizontal_alignment(Horizontal::Center)
|
||||||
|
|
@ -204,9 +222,17 @@ impl Application for Window {
|
||||||
.into(),
|
.into(),
|
||||||
separator!(1).into(),
|
separator!(1).into(),
|
||||||
content,
|
content,
|
||||||
])
|
])
|
||||||
.padding(4)
|
.padding(4)
|
||||||
.spacing(4)
|
.spacing(4))
|
||||||
|
.style(Container::Custom(|theme| container::Appearance {
|
||||||
|
text_color: Some(theme.cosmic().on_bg_color().into()),
|
||||||
|
background: Some(theme.extended_palette().background.base.color.into()),
|
||||||
|
border_radius: 12.0,
|
||||||
|
border_width: 0.0,
|
||||||
|
border_color: Color::TRANSPARENT,
|
||||||
|
}))
|
||||||
|
.align_y(Vertical::Center)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -226,6 +252,10 @@ impl Application for Window {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn style(&self) -> <Self::Theme as application::StyleSheet>::Style {
|
||||||
|
<Self::Theme as application::StyleSheet>::Style::Custom(|theme| Appearance { background_color: Color::from_rgba(0.0, 0.0, 0.0, 0.0), text_color: theme.cosmic().on_bg_color().into() })
|
||||||
|
}
|
||||||
|
|
||||||
fn view_window(&self, _: window::Id) -> Element<Message> {
|
fn view_window(&self, _: window::Id) -> Element<Message> {
|
||||||
// TODO use panel config crate after resolving version mismatch
|
// TODO use panel config crate after resolving version mismatch
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
use cosmic_panel_config::{PanelSize, PanelAnchor};
|
use cosmic_panel_config::{PanelSize, PanelAnchor};
|
||||||
use iced::{widget::Button, Rectangle};
|
use iced::{widget::{Button, self}, Rectangle, alignment::{Vertical, Horizontal}, Color, Element};
|
||||||
use iced_native::{command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner}};
|
use iced_native::{command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner}};
|
||||||
|
use iced_style::container::Appearance;
|
||||||
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
|
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
|
||||||
|
|
||||||
use crate::{button, widget::icon};
|
use crate::{button, widget::icon, theme::{Container, self}};
|
||||||
|
|
||||||
pub fn icon_button<'a, M: 'a, Renderer>() -> Button<'a, M, Renderer>
|
pub fn icon_button<'a, M: 'a, Renderer>() -> Button<'a, M, Renderer>
|
||||||
where
|
where
|
||||||
|
|
@ -62,4 +63,34 @@ pub fn get_popup_settings(parent: iced_native::window::Id, id: iced_native::wind
|
||||||
reactive: true,
|
reactive: true,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}, parent_size: None, grab: true }
|
}, parent_size: None, grab: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn popup_container<'a, Message, Renderer>(content: impl Into<Element<'a, Message, Renderer>>,) -> crate::widget::widget::Container<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Renderer: iced_native::Renderer,
|
||||||
|
<<Renderer as iced_native::Renderer>::Theme as iced_style::container::StyleSheet>::Style: From<theme::Container>, Renderer::Theme: widget::container::StyleSheet,
|
||||||
|
{
|
||||||
|
let anchor = std::env::var("COSMIC_PANEL_ANCHOR")
|
||||||
|
.ok()
|
||||||
|
.map(|size| match size.parse::<PanelAnchor>() {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(_) => PanelAnchor::Top,
|
||||||
|
})
|
||||||
|
.unwrap_or(PanelAnchor::Top);
|
||||||
|
let (valign, halign) = match anchor {
|
||||||
|
PanelAnchor::Left => (Vertical::Center, Horizontal::Left),
|
||||||
|
PanelAnchor::Right => (Vertical::Center, Horizontal::Right),
|
||||||
|
PanelAnchor::Top => (Vertical::Top, Horizontal::Center),
|
||||||
|
PanelAnchor::Bottom => (Vertical::Bottom, Horizontal::Center),
|
||||||
|
};
|
||||||
|
crate::widget::widget::container(content)
|
||||||
|
.style(Container::Custom(|theme| Appearance {
|
||||||
|
text_color: Some(theme.cosmic().on_bg_color().into()),
|
||||||
|
background: Some(theme.extended_palette().background.base.color.into()),
|
||||||
|
border_radius: 12.0,
|
||||||
|
border_width: 0.0,
|
||||||
|
border_color: Color::TRANSPARENT,
|
||||||
|
}))
|
||||||
|
.align_x(halign)
|
||||||
|
.align_y(valign)
|
||||||
}
|
}
|
||||||
|
|
@ -300,7 +300,7 @@ impl Default for Container {
|
||||||
|
|
||||||
impl From<fn(&Theme) -> container::Appearance> for Container {
|
impl From<fn(&Theme) -> container::Appearance> for Container {
|
||||||
fn from(f: fn(&Theme) -> container::Appearance) -> Self {
|
fn from(f: fn(&Theme) -> container::Appearance) -> Self {
|
||||||
Self::Custom(f)
|
Self::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue