fix: better button sizing so there are no gaps

This commit is contained in:
Ashley Wulber 2024-03-28 19:10:59 -04:00 committed by Ashley Wulber
parent a18d64add7
commit 71cb9e64ec
3 changed files with 116 additions and 74 deletions

View file

@ -8,14 +8,14 @@ use cosmic::iced::{
};
use cosmic::iced_core::alignment::{Horizontal, Vertical};
use cosmic::iced_style::application;
use cosmic::widget::{button, container, divider, grid, Button, Grid, Space};
use cosmic::widget::{button, container, divider, grid, horizontal_space, Button, Grid, Space};
use cosmic::{app, applet::cosmic_panel_config::PanelAnchor, Command};
use cosmic::{
widget::{icon, rectangle_tracker::*},
Element, Theme,
};
use chrono::{DateTime, Datelike, DurationRound, Local, Months, NaiveDate, Timelike, Weekday};
use chrono::{DateTime, Datelike, DurationRound, Local, Months, NaiveDate, Weekday};
use crate::fl;
use crate::time::get_calender_first;
@ -209,36 +209,58 @@ impl cosmic::Application for Window {
}
fn view(&self) -> Element<Message> {
let button = cosmic::widget::button(
if matches!(
self.core.applet.anchor,
PanelAnchor::Top | PanelAnchor::Bottom
) {
Element::from(
let horizontal = matches!(
self.core.applet.anchor,
PanelAnchor::Top | PanelAnchor::Bottom
);
let button = cosmic::widget::button(if horizontal {
Element::from(
row!(
cosmic::widget::text(self.now.format("%b %-d %-I:%M %p").to_string()).size(14),
container(vertical_space(Length::Fixed(
(self.core.applet.suggested_size().1
+ 2 * self.core.applet.suggested_padding())
as f32
)))
)
} else {
let mut date_time_col = column![
icon::from_name("emoji-recent-symbolic")
.size(self.core.applet.suggested_size().0)
.symbolic(true),
text(self.now.format("%I").to_string()).size(14),
text(self.now.format("%M").to_string()).size(14),
text(self.now.format("%p").to_string()).size(14),
vertical_space(Length::Fixed(4.0)),
// TODO better calendar icon?
icon::from_name("calendar-go-today-symbolic")
.size(self.core.applet.suggested_size().0)
.symbolic(true),
]
.align_items(Alignment::Center)
.spacing(4);
for d in self.now.format("%x").to_string().split('/') {
date_time_col = date_time_col.push(text(d.to_string()).size(14));
}
date_time_col.into()
},
)
.align_items(Alignment::Center),
)
} else {
let mut date_time_col = column![
icon::from_name("emoji-recent-symbolic")
.size(self.core.applet.suggested_size().0)
.symbolic(true),
text(self.now.format("%I").to_string()).size(14),
text(self.now.format("%M").to_string()).size(14),
text(self.now.format("%p").to_string()).size(14),
vertical_space(Length::Fixed(4.0)),
// TODO better calendar icon?
icon::from_name("calendar-go-today-symbolic")
.size(self.core.applet.suggested_size().0)
.symbolic(true),
]
.align_items(Alignment::Center)
.spacing(4);
for d in self.now.format("%x").to_string().split('/') {
date_time_col = date_time_col.push(text(d.to_string()).size(14));
}
Element::from(
column!(
date_time_col,
horizontal_space(Length::Fixed(
(self.core.applet.suggested_size().0
+ 2 * self.core.applet.suggested_padding())
as f32
))
)
.align_items(Alignment::Center),
)
})
.padding(if horizontal {
[0, self.core.applet.suggested_padding()]
} else {
[self.core.applet.suggested_padding(), 0]
})
.on_press(Message::TogglePopup)
.style(cosmic::theme::Button::AppletIcon);

View file

@ -6,6 +6,7 @@ use cosmic::iced::widget::{button, column, container, row, text};
use cosmic::iced::{Event::Mouse, Length, Subscription};
use cosmic::iced_core::{Background, Border};
use cosmic::iced_style::application;
use cosmic::widget::{horizontal_space, vertical_space};
use cosmic::{applet::cosmic_panel_config::PanelAnchor, font::FONT_BOLD, Command};
use cosmic::{Element, Theme};
@ -121,30 +122,41 @@ impl cosmic::Application for IcedWorkspacesApplet {
if self.workspaces.is_empty() {
return row![].padding(8).into();
}
let horizontal = matches!(
self.core.applet.anchor,
PanelAnchor::Top | PanelAnchor::Bottom
);
let buttons = self.workspaces.iter().filter_map(|w| {
let btn = button(
text(w.0.clone())
.font(FONT_BOLD)
.size(16)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.width(Length::Fill)
.height(Length::Fill),
let content = text(w.0.clone()).font(FONT_BOLD).size(16);
let content = row!(
content,
vertical_space(Length::Fixed(
(self.core.applet.suggested_size().1 + 2 * self.core.applet.suggested_padding())
as f32
))
)
.width(Length::Fixed(
self.core.applet.suggested_size().0 as f32
+ match self.layout {
Layout::Row => 20.0,
Layout::Column => 16.0,
},
))
.height(Length::Fixed(
self.core.applet.suggested_size().0 as f32
+ match self.layout {
Layout::Row => 16.0,
Layout::Column => 20.0,
},
))
.align_items(cosmic::iced::Alignment::Center);
let content = column!(
content,
horizontal_space(Length::Fixed(
(self.core.applet.suggested_size().0 + 2 * self.core.applet.suggested_padding())
as f32
))
)
.align_items(cosmic::iced::Alignment::Center);
let btn = button(
container(content)
.align_x(Horizontal::Center)
.align_y(Vertical::Center),
)
.padding(if horizontal {
[0, self.core.applet.suggested_padding()]
} else {
[self.core.applet.suggested_padding(), 0]
})
.on_press(Message::WorkspacePressed(w.2.clone()))
.padding(0);
@ -217,25 +229,11 @@ impl cosmic::Application for IcedWorkspacesApplet {
)
});
let layout_section: Element<_> = match self.layout {
Layout::Row => row(buttons)
.width(Length::Shrink)
.height(Length::Shrink)
.padding([0, 4])
.spacing(4)
.into(),
Layout::Column => column(buttons)
.width(Length::Shrink)
.height(Length::Shrink)
.padding([4, 0])
.spacing(4)
.into(),
Layout::Row => row(buttons).spacing(4).into(),
Layout::Column => column(buttons).spacing(4).into(),
};
container(layout_section)
.width(Length::Shrink)
.height(Length::Shrink)
.padding(0)
.into()
container(layout_section).padding(0).into()
}
fn subscription(&self) -> Subscription<Message> {

View file

@ -1,4 +1,7 @@
use cosmic::iced_widget::text;
use cosmic::applet::cosmic_panel_config::PanelAnchor;
use cosmic::iced::Length;
use cosmic::iced_widget::{row, text};
use cosmic::widget::vertical_space;
use cosmic::{app, iced, iced_style::application, theme::Theme};
use freedesktop_desktop_entry::DesktopEntry;
use std::{env, fs, process::Command};
@ -52,10 +55,29 @@ impl cosmic::Application for Button {
}
fn view(&self) -> cosmic::Element<Msg> {
cosmic::widget::button(text(&self.desktop.name).size(14.0))
.style(cosmic::theme::Button::AppletIcon)
.on_press(Msg::Press)
.into()
if matches!(
self.core.applet.anchor,
PanelAnchor::Left | PanelAnchor::Right
) && self.desktop.icon.is_some()
{
self.core
.applet
.icon_button(self.desktop.icon.as_ref().unwrap())
} else {
let content = row!(
text(&self.desktop.name).size(14.0),
vertical_space(Length::Fixed(
(self.core.applet.suggested_size().1 + 2 * self.core.applet.suggested_padding())
as f32
))
)
.align_items(iced::Alignment::Center);
cosmic::widget::button(content)
.padding([0, self.core.applet.suggested_padding()])
.style(cosmic::theme::Button::AppletIcon)
}
.on_press(Msg::Press)
.into()
}
}