Skip to content

Commit

Permalink
add pages
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Dec 14, 2024
1 parent 9242ae0 commit 190cdf6
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- update libcosmic
- add pages

## [0.1.0] - 2024-11-19

Expand Down
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ constcat = "0.5"
nucleo = "0.5"
futures = "0.3"
include_dir = "0.7"
itertools = "0.13.0"
[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic"
default-features = false
Expand Down
6 changes: 6 additions & 0 deletions res/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"description": "Reset the database at each login",
"default": false,
"type": "boolean"
},
"maximum_entries_by_page": {
"default": 50,
"type": "integer",
"format": "uint32",
"minimum": 1.0
}
},
"X_CONFIGURATOR_SOURCE_HOME_PATH": ".config/cosmic/io.github.wiiznokes.cosmic-ext-applet-clipboard-manager/v3",
Expand Down
1 change: 1 addition & 0 deletions res/icons/arrow_back_ios_new24.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions res/icons/arrow_forward_ios24.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct AppState {
pub db: Db,
pub clipboard_state: ClipboardState,
pub focused: usize,
pub page: usize,
pub qr_code: Option<Result<qr_code::Data, ()>>,
last_quit: Option<(i64, PopupKind)>,
}
Expand Down Expand Up @@ -109,6 +110,7 @@ impl AppState {

fn close_popup(&mut self) -> Task<AppMsg> {
self.focused = 0;
self.page = 0;
self.db.set_query_and_search("".into());

if let Some(popup) = self.popup.take() {
Expand Down Expand Up @@ -224,6 +226,7 @@ impl cosmic::Application for AppState {
qr_code: None,
config,
last_quit: None,
page: 0,
};

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -396,6 +399,14 @@ impl cosmic::Application for AppState {
error!("{err}");
}
}
AppMsg::NextPage => {
self.page += 1;
self.focused = self.page * self.config.maximum_entries_by_page.get() as usize;
}
AppMsg::PreviousPage => {
self.page -= 1;
self.focused = self.page * self.config.maximum_entries_by_page.get() as usize;
}
}
Task::none()
}
Expand Down
8 changes: 7 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{sync::atomic::AtomicBool, time::Duration};
use std::{
num::{NonZero, NonZeroU32},
sync::atomic::AtomicBool,
time::Duration,
};

#[cfg(test)]
use configurator_schema::schemars;
Expand Down Expand Up @@ -29,6 +33,7 @@ pub struct Config {
pub horizontal: bool,
/// Reset the database at each login
pub unique_session: bool,
pub maximum_entries_by_page: NonZeroU32,
}

impl Config {
Expand All @@ -46,6 +51,7 @@ impl Default for Config {
maximum_entries_number: Some(500),
horizontal: false,
unique_session: false,
maximum_entries_by_page: NonZero::new(50).unwrap(),
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/icon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#![allow(dead_code)]

use cosmic::{
iced_core::Length,
widget::{self, icon::Handle, Icon, IconButton},
};

pub static ICON_LENGTH: Length = Length::Fixed(25.0);

#[macro_export]
macro_rules! icon_handle {
($name:literal) => {{
let bytes = include_bytes!(concat!("../res/icons/", $name, ".svg"));
cosmic::widget::icon::from_svg_bytes(bytes).symbolic(true)
}};
}

#[macro_export]
macro_rules! icon {
($name:literal) => {{
use $crate::icon::ICON_LENGTH;
use $crate::icon_handle;

cosmic::widget::icon::icon(icon_handle!($name))
.height(ICON_LENGTH)
.width(ICON_LENGTH)
}};
}
#[macro_export]
macro_rules! icon_button {
($name:literal) => {{
use $crate::icon_handle;
cosmic::widget::button::icon(icon_handle!($name))
}};
}

pub fn icon_from_handle(handle: Handle) -> Icon {
widget::icon::icon(handle)
.height(ICON_LENGTH)
.width(ICON_LENGTH)
}

pub fn icon_button_from_handle<'a, M>(handle: Handle) -> IconButton<'a, M> {
cosmic::widget::button::icon(handle)
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod app;
mod clipboard;
mod config;
mod db;
mod icon;
mod localize;
mod message;
mod navigation;
Expand Down
2 changes: 2 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub enum AppMsg {
Config(ConfigMsg),
AddFavorite(Entry),
RemoveFavorite(Entry),
NextPage,
PreviousPage,
}

#[derive(Clone, Debug)]
Expand Down
103 changes: 55 additions & 48 deletions src/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, cmp::min};

use cosmic::{
iced::{Alignment, Length, Padding},
iced::{padding, Alignment, Length, Padding},
iced_widget::scrollable::{Direction, Scrollbar},
theme::Button,
widget::{
Expand All @@ -12,23 +12,16 @@ use cosmic::{
},
Element,
};
use itertools::Itertools;

use crate::{
app::AppState,
db::{Content, Entry},
fl,
fl, icon_button,
message::{AppMsg, ConfigMsg},
utils::formatted_value,
};

#[macro_export]
macro_rules! icon {
($name:literal) => {{
let bytes = include_bytes!(concat!("../../res/icons/", $name, "px.svg"));
cosmic::widget::icon::from_svg_bytes(bytes)
}};
}

impl AppState {
pub fn quick_settings_view(&self) -> Element<'_, AppMsg> {
fn toggle_settings<'a>(
Expand Down Expand Up @@ -70,8 +63,9 @@ impl AppState {
column()
.push(self.top_bar())
.push(self.content())
// .push(self.page_actions())
.spacing(20)
.padding(10)
// .padding(10)
.align_x(Alignment::Center)
// .width(Length::Fill)
// .height(Length::Fill)
Expand All @@ -87,18 +81,39 @@ impl AppState {
})
.into()
}
pub fn page_count(&self) -> usize {
self.db.len() / self.config.maximum_entries_by_page.get() as usize
}

fn top_bar(&self) -> Element<'_, AppMsg> {
let content: Element<_> = match self.qr_code.is_none() {
true => text_input::search_input(fl!("search_entries"), self.db.query())
.always_active()
.on_input(AppMsg::Search)
.on_paste(AppMsg::Search)
.on_clear(AppMsg::Search("".into()))
.width(match self.config.horizontal {
true => Length::Fixed(250f32),
false => Length::Fill,
})
true => row()
.push(
text_input::search_input(fl!("search_entries"), self.db.query())
.always_active()
.on_input(AppMsg::Search)
.on_paste(AppMsg::Search)
.on_clear(AppMsg::Search("".into()))
.width(match self.config.horizontal {
true => Length::Fixed(250f32),
false => Length::Fill,
}),
)
.push(horizontal_space().width(5))
.push(
icon_button!("arrow_back_ios_new24").on_press_maybe(if self.page > 0 {
Some(AppMsg::PreviousPage)
} else {
None
}),
)
.push(icon_button!("arrow_forward_ios24").on_press_maybe(
if self.page < self.page_count() {
Some(AppMsg::NextPage)
} else {
None
},
))
.into(),
false => button::text(fl!("return_to_clipboard"))
.on_press(AppMsg::ReturnToClipboard)
Expand All @@ -110,25 +125,30 @@ impl AppState {
};

container(content)
.padding(Padding::new(10f32).bottom(0))
.padding(Padding::new(15f32).bottom(0))
.into()
}

fn content(&self) -> Element<'_, AppMsg> {
match &self.qr_code {
let content: Element<_> = match &self.qr_code {
Some(qr_code) => {
let qr_code_content: Element<_> = match qr_code {
Ok(c) => widget::qr_code(c).into(),
Err(()) => text(fl!("qr_code_error")).into(),
};

return container(qr_code_content).center(Length::Fill).into();
container(qr_code_content).center(Length::Fill).into()
}
None => {
let maximum_entries_by_page = self.config.maximum_entries_by_page.get() as usize;
let range =
self.page * maximum_entries_by_page..(self.page + 1) * maximum_entries_by_page;

let entries_view: Vec<_> = if self.db.query().is_empty() {
self.db
.iter()
.enumerate()
.get(range)
.filter_map(|(pos, data)| match data.get_content() {
Ok(c) => match c {
Content::Text(text) => {
Expand All @@ -145,10 +165,10 @@ impl AppState {
})
.collect()
} else {
self.db
.search_iter()
.enumerate()
.filter_map(|(pos, (data, indices))| match data.get_content() {
let mut vec = Vec::with_capacity(maximum_entries_by_page + 1);

vec.extend(self.db.search_iter().enumerate().get(range).filter_map(
|(pos, (data, indices))| match data.get_content() {
Ok(c) => match c {
Content::Text(text) => self.text_entry_with_indices(
data,
Expand All @@ -164,46 +184,33 @@ impl AppState {
}
},
Err(_) => None,
})
.collect()
},
));
vec
};

if self.config.horizontal {
// try to fix scroll bar
let padding = Padding {
top: 0f32,
right: 10f32,
bottom: 20f32,
left: 10f32,
};

let column = row::with_children(entries_view)
.spacing(5f32)
.padding(padding);
.padding(padding::bottom(10));

scrollable(column)
.direction(Direction::Horizontal(Scrollbar::default()))
.into()
} else {
// try to fix scroll bar
let padding = Padding {
top: 0f32,
right: 20f32,
bottom: 0f32,
left: 10f32,
};

let column = column::with_children(entries_view)
.spacing(5f32)
.padding(padding);
.padding(padding::right(10));

scrollable(column)
// XXX: why ?
.height(Length::FillPortion(2))
.into()
}
}
}
};

container(content).padding(padding::all(20).top(0)).into()
}

fn image_entry<'a>(
Expand Down

0 comments on commit 190cdf6

Please sign in to comment.