refactor(wallpaper): integrate flexrow widget in libcosmic

This commit is contained in:
Michael Aaron Murphy 2023-06-24 00:01:48 +02:00 committed by Michael Murphy
parent c933f80cde
commit a109caf0cc
4 changed files with 94 additions and 129 deletions

View file

@ -15,8 +15,8 @@ use cosmic::widget::{
segmented_button::{self, SingleSelectModel},
settings, toggler,
};
use cosmic::{iced::alignment::Horizontal, iced::Length, Element};
use cosmic::{iced_core::alignment::Vertical, iced_runtime::core::image::Handle as ImageHandle};
use cosmic::{iced::Length, Element};
use cosmic::{iced_core::alignment, iced_runtime::core::image::Handle as ImageHandle};
use cosmic_settings_desktop::wallpaper::{self, Entry, ScalingMode};
use cosmic_settings_page::Section;
use cosmic_settings_page::{self as page, section};
@ -521,11 +521,12 @@ pub fn settings() -> Section<crate::pages::Message> {
children.push(if page.config.same_on_all {
cosmic::widget::text(fl!("all-displays"))
.font(cosmic::font::FONT_SEMIBOLD)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.horizontal_alignment(alignment::Horizontal::Center)
.vertical_alignment(alignment::Vertical::Center)
.width(Length::Fill)
.height(Length::Fill)
.apply(cosmic::iced::widget::container)
.width(Length::Fill)
.height(Length::Fixed(32.0))
.into()
} else {

View file

@ -4,17 +4,14 @@
use super::Message;
use apply::Apply;
use cosmic::iced_core::{self, gradient::Linear, Background, BorderRadius, Color, Degrees};
use cosmic::iced_core::{alignment, Length};
use cosmic::iced_runtime::core::image::Handle as ImageHandle;
use cosmic::{
iced,
iced_widget::{column, row},
Element,
};
use cosmic::{iced, Element};
use cosmic_settings_desktop::wallpaper;
use slotmap::DefaultKey;
const COLOR_WIDTH: u16 = 70;
const WALLPAPER_WIDTH: f32 = 158.0;
const WALLPAPER_WIDTH: u16 = 158;
const COLUMN_SPACING: u16 = 12;
const ROW_SPACING: u16 = 16;
@ -69,78 +66,39 @@ pub fn color_image(
/// Color selection list
pub fn color_select_options() -> Element<'static, Message> {
cosmic::iced::widget::responsive(|size| {
let items_per_row =
flex_row_items(size.width, COLOR_WIDTH as f32, ROW_SPACING as f32, 8) as usize;
flex_select_row(|vec, _size| {
let elements = wallpaper::DEFAULT_COLORS.iter().cloned().map(color_button);
let mut color_column = Vec::with_capacity(wallpaper::DEFAULT_COLORS.len() / items_per_row);
let mut colors = wallpaper::DEFAULT_COLORS.iter();
vec.extend(elements);
while let Some(color) = colors.next() {
let mut color_row = Vec::with_capacity(items_per_row);
color_row.push(color_button(color.clone()));
for color in colors.by_ref().take(items_per_row - 1) {
color_row.push(color_button(color.clone()));
}
color_column.push(row(color_row).spacing(ROW_SPACING).into());
}
column(color_column)
.spacing(COLUMN_SPACING)
.padding(0)
.apply(cosmic::iced::widget::container)
.align_x(iced_core::alignment::Horizontal::Center)
.width(size.width)
.into()
COLOR_WIDTH
})
.into()
}
fn flex_row_items(available: f32, item_width: f32, spacing: f32, max_items: u32) -> u32 {
let mut items = 2;
while items <= max_items && available >= (item_width + spacing) * items as f32 - spacing {
items += 1;
}
items - 1
}
/// Background selection list
pub fn wallpaper_select_options(page: &super::Page) -> Element<Message> {
cosmic::iced::widget::responsive(|size| {
let items_per_row =
flex_row_items(size.width, WALLPAPER_WIDTH, ROW_SPACING as f32, 4) as usize;
flex_select_row(move |vec, _size| {
let elements = page
.selection
.selection_handles
.iter()
.map(|(id, handle)| wallpaper_button(handle, id));
let mut image_column =
Vec::with_capacity(page.selection.selection_handles.len() / items_per_row);
vec.extend(elements);
let mut image_handles = page.selection.selection_handles.iter();
while let Some((id, handle)) = image_handles.next() {
let mut image_row = Vec::with_capacity(items_per_row);
image_row.push(wallpaper_button(handle, id));
for (id, handle) in image_handles.by_ref().take(items_per_row - 1) {
image_row.push(wallpaper_button(handle, id));
}
image_column.push(row(image_row).spacing(ROW_SPACING).into());
}
column(image_column)
.spacing(COLUMN_SPACING)
.padding(0)
.apply(cosmic::iced::widget::container)
.align_x(iced_core::alignment::Horizontal::Center)
.width(size.width)
.into()
WALLPAPER_WIDTH
})
.into()
}
fn flex_select_row<'a>(
elements: impl Fn(&mut Vec<Element<'a, Message>>, iced_core::Size) -> u16 + 'a,
) -> Element<'a, Message> {
cosmic::widget::flex_row(elements)
.column_spacing(COLUMN_SPACING)
.row_spacing(ROW_SPACING)
.width(Length::Fill)
.align_x(alignment::Horizontal::Center)
.into()
}
fn wallpaper_button(handle: &ImageHandle, id: DefaultKey) -> Element<Message> {