2023-01-26 22:16:13 +01:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// 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 {
|
2023-05-30 12:03:15 -04:00
|
|
|
pub input_id: iced_core::id::Id,
|
2023-01-26 22:16:13 +01:00
|
|
|
pub phrase: String,
|
|
|
|
|
pub state: State,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Model {
|
|
|
|
|
/// Focuses the search field.
|
|
|
|
|
pub fn focus<Message: 'static>(&mut self) -> crate::iced::Command<Message> {
|
|
|
|
|
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 {
|
2023-05-30 12:03:15 -04:00
|
|
|
input_id: iced_core::id::Id::unique(),
|
2023-01-26 22:16:13 +01:00
|
|
|
phrase: String::with_capacity(32),
|
|
|
|
|
state: State::Inactive,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|