From 71cb9e64ec2dff53229688aea32d22f65653b5be Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 28 Mar 2024 19:10:59 -0400 Subject: [PATCH] fix: better button sizing so there are no gaps --- cosmic-applet-time/src/window.rs | 82 ++++++++++++------- .../src/components/app.rs | 76 +++++++++-------- cosmic-panel-button/src/main.rs | 32 ++++++-- 3 files changed, 116 insertions(+), 74 deletions(-) diff --git a/cosmic-applet-time/src/window.rs b/cosmic-applet-time/src/window.rs index 1ad34f61..87c7bc42 100644 --- a/cosmic-applet-time/src/window.rs +++ b/cosmic-applet-time/src/window.rs @@ -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 { - 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); diff --git a/cosmic-applet-workspaces/src/components/app.rs b/cosmic-applet-workspaces/src/components/app.rs index 8d86649c..094809d0 100644 --- a/cosmic-applet-workspaces/src/components/app.rs +++ b/cosmic-applet-workspaces/src/components/app.rs @@ -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 { diff --git a/cosmic-panel-button/src/main.rs b/cosmic-panel-button/src/main.rs index 24016af0..ad9403a0 100644 --- a/cosmic-panel-button/src/main.rs +++ b/cosmic-panel-button/src/main.rs @@ -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 { - 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() } }