From b09b3db81a7a22171ce4d9bb4948ce57007cef3d Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Mon, 22 Jan 2024 08:48:45 +0100 Subject: [PATCH] chore(widget): remove redundant `search` widget The `text_input` widget's search variant replaced this. --- src/widget/mod.rs | 2 - src/widget/search/field.rs | 88 -------------------------------- src/widget/search/mod.rs | 98 ------------------------------------ src/widget/search/model.rs | 36 ------------- src/widget/search/search.svg | 11 ---- 5 files changed, 235 deletions(-) delete mode 100644 src/widget/search/field.rs delete mode 100644 src/widget/search/mod.rs delete mode 100644 src/widget/search/model.rs delete mode 100644 src/widget/search/search.svg diff --git a/src/widget/mod.rs b/src/widget/mod.rs index 3684b954..b1b8bb43 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -163,8 +163,6 @@ pub mod row { mod scrollable; pub use scrollable::*; -pub mod search; - pub mod segmented_button; pub mod segmented_selection; diff --git a/src/widget/search/field.rs b/src/widget/search/field.rs deleted file mode 100644 index d1176559..00000000 --- a/src/widget/search/field.rs +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2023 System76 -// SPDX-License-Identifier: MPL-2.0 - -use crate::iced::{Background, Length}; -use crate::widget::{container, icon, row, text_input}; -use apply::Apply; - -/// A search field for COSMIC applications. -pub fn field( - id: iced_core::id::Id, - phrase: &str, - on_change: fn(String) -> Message, - on_clear: Message, - on_submit: Option, -) -> Field { - Field { - id, - phrase, - on_change, - on_clear, - on_submit, - } -} - -/// A search field for COSMIC applications. -#[must_use] -pub struct Field<'a, Message: 'static + Clone> { - id: iced_core::id::Id, - phrase: &'a str, - on_change: fn(String) -> Message, - on_clear: Message, - on_submit: Option, -} - -impl<'a, Message: 'static + Clone> Field<'a, Message> { - pub fn into_element(mut self) -> crate::Element<'a, Message> { - let input = text_input("", self.phrase) - .on_input(self.on_change) - .width(Length::Fill) - .id(self.id) - .on_submit_maybe(self.on_submit.take()); - - row::with_capacity(3) - .push( - icon::from_svg_bytes(&include_bytes!("search.svg")[..]) - .symbolic(true) - .icon() - .size(16), - ) - .push(input) - .push(clear_button().on_press(self.on_clear)) - .width(Length::Fixed(300.0)) - .height(Length::Fixed(38.0)) - .padding([0, 16]) - .spacing(8) - .align_items(iced::Alignment::Center) - .apply(container) - .style(crate::theme::Container::custom(active_style)) - .into() - } -} - -impl<'a, Message: 'static + Clone> From> for crate::Element<'a, Message> { - fn from(field: Field<'a, Message>) -> Self { - field.into_element() - } -} - -fn clear_button() -> crate::widget::IconButton<'static, Message> { - icon::from_name("edit-clear-symbolic") - .size(16) - .apply(crate::widget::button::icon) -} - -#[allow(clippy::trivially_copy_pass_by_ref)] -fn active_style(theme: &crate::Theme) -> container::Appearance { - let cosmic = &theme.cosmic(); - let mut neutral_7 = cosmic.palette.neutral_7; - neutral_7.alpha = 0.25; - iced::widget::container::Appearance { - icon_color: Some(cosmic.palette.neutral_9.into()), - text_color: Some(cosmic.palette.neutral_9.into()), - background: Some(Background::Color(neutral_7.into())), - border_radius: cosmic.corner_radii.radius_m.into(), - border_width: 2.0, - border_color: cosmic.accent.focus.into(), - } -} diff --git a/src/widget/search/mod.rs b/src/widget/search/mod.rs deleted file mode 100644 index b7f1c794..00000000 --- a/src/widget/search/mod.rs +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2023 System76 -// SPDX-License-Identifier: MPL-2.0 - -//! A COSMIC search widget -//! -//! ## Example -//! -//! Store the model in the application: -//! -//! ```ignore -//! App { -//! search: search::Model::default() -//! } -//! ``` -//! -//! Generate the element in the view: -//! -//! ```ignore -//! let search_field = search::search(&self.search, Message::Search); -//! ``` -//! -//! Handle messages in the update method: -//! -//! ```ignore -//! match message { -//! Message::Search(search::Message::Activate) => { -//! // Returns command to focus the text input. -//! return self.search.focus(); -//! } -//! Message::Search(search::Message::Changed) => { -//! self.search.phrase = phrase; -//! self.search_changed(); -//! } -//! Message::Search(search::Message::Clear) => { -//! self.search_clear(); -//! }, -//! Message::Search(search::Message::Submit) => { -//! self.search_submit(); -//! } -//! } - -mod field; -mod model; - -mod button { - use crate::widget::{container, icon}; - use apply::Apply; - - /// A search button which converts to a search [`field`] on click. - #[must_use] - pub fn button(on_press: Message) -> crate::Element<'static, Message> { - icon::from_svg_bytes(&include_bytes!("search.svg")[..]) - .symbolic(true) - .apply(crate::widget::button::icon) - .on_press(on_press) - .apply(container) - .padding([0, 0, 0, 11]) - .into() - } -} - -pub use button::button; -pub use field::{field, Field}; -pub use model::Model; - -/// Creates the COSMIC search field widget -/// -/// A button is displayed when inactive, and the search field when active. -pub fn search(model: &Model, on_emit: fn(Message) -> M) -> crate::Element { - let element = match model.state { - State::Active => field( - model.input_id.clone(), - &model.phrase, - Message::Changed, - Message::Clear, - Some(Message::Submit), - ) - .into(), - - State::Inactive => button(Message::Activate), - }; - - element.map(on_emit) -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub enum Message { - Activate, - Changed(String), - Clear, - Submit, -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum State { - Active, - Inactive, -} diff --git a/src/widget/search/model.rs b/src/widget/search/model.rs deleted file mode 100644 index 632c05b6..00000000 --- a/src/widget/search/model.rs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2023 System76 -// SPDX-License-Identifier: MPL-2.0 - -use super::State; -use crate::iced; - -/// A model for managing the state of a search widget. -pub struct Model { - pub input_id: iced_core::id::Id, - pub phrase: String, - pub state: State, -} - -impl Model { - /// Focuses the search field. - pub fn focus(&mut self) -> crate::iced::Command { - self.state = State::Active; - iced::widget::text_input::focus(self.input_id.clone()) - } - - /// Check if the search field is currently active. - #[must_use] - pub fn is_active(&self) -> bool { - self.state == State::Active - } -} - -impl Default for Model { - fn default() -> Self { - Self { - input_id: iced_core::id::Id::unique(), - phrase: String::with_capacity(32), - state: State::Inactive, - } - } -} diff --git a/src/widget/search/search.svg b/src/widget/search/search.svg deleted file mode 100644 index 33b8e88e..00000000 --- a/src/widget/search/search.svg +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - -