feat: popup container helper

This commit is contained in:
Ashley Wulber 2022-11-14 16:33:05 +01:00
parent 49f0f34270
commit 00eed9c741
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
5 changed files with 112 additions and 51 deletions

View file

@ -7,7 +7,7 @@ edition = "2021"
name = "cosmic"
[features]
default = ["iced_winit", "wgpu"]
default = ["wayland"]
debug = ["iced/debug"]
wayland = ["iced/wayland"]
wgpu = ["iced/wgpu"]

View file

@ -4,8 +4,8 @@ mod window;
use cosmic::{
iced::{sctk_settings::InitialSurface, Application},
iced_native::window::Settings,
iced_native::command::platform_specific::wayland::window::SctkWindowSettings,
iced_native::window::Settings,
settings,
};
use cosmic_panel_config::PanelSize;

View file

@ -1,7 +1,10 @@
use crate::dbus::{self, PowerDaemonProxy};
use crate::graphics::{get_current_graphics, set_graphics, Graphics};
use cosmic::applet::{get_popup_settings, icon_button};
use cosmic::iced_style::application::{self, Appearance};
use cosmic::separator;
use cosmic::theme::Container;
use cosmic::widget::widget::container;
use cosmic::{
iced::widget::{column, radio, text},
iced::{self, Application, Command, Length},
@ -10,17 +13,23 @@ use cosmic::{
Element,
};
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 zbus::Connection;
#[derive(Default, Clone, Copy)]
#[derive(Clone, Copy)]
enum State {
#[default]
SelectGraphicsMode,
SelectGraphicsMode(bool),
SettingGraphicsMode(Graphics),
}
impl Default for State {
fn default() -> Self {
Self::SelectGraphicsMode(false)
}
}
#[derive(Default)]
pub struct Window {
popup: Option<window::Id>,
@ -83,7 +92,6 @@ impl Application for Window {
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
match message {
Message::SelectGraphicsMode(new_graphics_mode) => {
dbg!(new_graphics_mode);
if let Some((_, proxy)) = self.dbus.as_ref() {
self.state = State::SettingGraphicsMode(new_graphics_mode);
return Command::perform(
@ -96,9 +104,8 @@ impl Application for Window {
}
Message::AppliedGraphicsMode(g) => {
if let Some(g) = g {
dbg!(g);
self.graphics_mode.replace(g);
self.state = State::SelectGraphicsMode;
self.state = State::SelectGraphicsMode(true);
}
}
Message::TogglePopup => {
@ -115,7 +122,9 @@ impl Application for Window {
|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));
return Command::batch(commands);
}
@ -136,7 +145,6 @@ impl Application for Window {
);
}
Message::CurrentGraphics(g) => {
dbg!(g);
if let Some(g) = g {
self.graphics_mode.replace(g);
}
@ -147,39 +155,49 @@ impl Application for Window {
fn view_popup(&self, _: window::Id) -> Element<Message> {
let content = match self.state {
State::SelectGraphicsMode => column(vec![
radio(
"Integrated Graphics",
Graphics::Integrated,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
radio(
"Nvidia Graphics",
Graphics::Nvidia,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
radio(
"Hybrid Graphics",
Graphics::Hybrid,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
radio(
"Compute Graphics",
Graphics::Compute,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
])
.padding([8, 0])
.spacing(8)
.into(),
State::SelectGraphicsMode(pending_restart) => {
let mut content_list = vec![
radio(
"Integrated Graphics",
Graphics::Integrated,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
radio(
"Nvidia Graphics",
Graphics::Nvidia,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
radio(
"Hybrid Graphics",
Graphics::Hybrid,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
radio(
"Compute Graphics",
Graphics::Compute,
self.graphics_mode,
|g| Message::SelectGraphicsMode(g),
)
.into(),
];
if pending_restart {
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) => {
let graphics_str = match graphics {
Graphics::Integrated => "integrated",
@ -196,7 +214,7 @@ impl Application for Window {
.into()
}
};
column(vec![
container(column(vec![
text("Graphics Mode")
.width(Length::Fill)
.horizontal_alignment(Horizontal::Center)
@ -204,9 +222,17 @@ impl Application for Window {
.into(),
separator!(1).into(),
content,
])
.padding(4)
.spacing(4)
])
.padding(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()
}
@ -226,6 +252,10 @@ impl Application for Window {
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> {
// TODO use panel config crate after resolving version mismatch

View file

@ -1,9 +1,10 @@
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_style::container::Appearance;
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>
where
@ -62,4 +63,34 @@ pub fn get_popup_settings(parent: iced_native::window::Id, id: iced_native::wind
reactive: true,
..Default::default()
}, 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)
}

View file

@ -300,7 +300,7 @@ impl Default for Container {
impl From<fn(&Theme) -> container::Appearance> for Container {
fn from(f: fn(&Theme) -> container::Appearance) -> Self {
Self::Custom(f)
Self::default()
}
}