add cursor with selection
[?]
Feb 8, 2025, 8:11 PM
WT3GA27PQ2AOAIGK65O3Q4DMX4AZDVNULBLRL6GF4QW6QCASUEAACDependencies
- [2]
6YZAVBWUInitial commit - [3]
KLR5FRIBadd fs state read/write of repos - [4]
IQDCHWCPload a pijul repo - [5]
3GZPRZXCs/-/_ in crate paths - [6]
SWWE2R6Mdisplay basic repo stuff
Change contents
- edit in crates/libflowers_client/src/lib.rs at line 1
pub mod cursor; - file addition: cursor.rs[5.36]
#[derive(Debug, Default)]pub struct State {pub selection: Option<Selection>,}#[derive(Debug, Clone)]pub enum Selection {Channel,File(usize),} - edit in crates/libflowers/src/prelude.rs at line 8
#[inline]pub fn default<T: Default>() -> T {Default::default()} - edit in crates/flowers_ui/src/main.rs at line 1
use std::io::Cursor; - replacement in crates/flowers_ui/src/main.rs at line 5
use libflowers_client::repo;use libflowers_client::{cursor, repo}; - replacement in crates/flowers_ui/src/main.rs at line 7
use iced::widget::{column, horizontal_rule, row, text};use iced::{Element, Length, Task, Theme};use iced::widget::{button, column, horizontal_rule, row, text};use iced::{Border, Color, Element, Length, Subscription, Task, Theme}; - edit in crates/flowers_ui/src/main.rs at line 12
.subscription(subs) - replacement in crates/flowers_ui/src/main.rs at line 21
(State { repo }, Task::none())let cursor = cursor::State::default();(State { repo, cursor }, Task::none()) - edit in crates/flowers_ui/src/main.rs at line 28
cursor: cursor::State, - replacement in crates/flowers_ui/src/main.rs at line 36
enum Message {}enum Message {CursorDown,CursorUp,CursorSelect(cursor::Selection),} - replacement in crates/flowers_ui/src/main.rs at line 43
match message {}match message {Message::CursorDown => {state.cursor.selection =Some(match state.cursor.selection.as_ref() {Some(cursor::Selection::Channel) => {if state.repo.changed_files.is_empty() {cursor::Selection::Channel} else {cursor::Selection::File(0)}}Some(cursor::Selection::File(ix)) => {if state.repo.changed_files.len().saturating_sub(1)== *ix{cursor::Selection::Channel} else {cursor::Selection::File(ix + 1)}}None => cursor::Selection::Channel,})}Message::CursorUp => {state.cursor.selection =Some(match state.cursor.selection.as_ref() {Some(cursor::Selection::Channel) | None => {if state.repo.changed_files.is_empty() {cursor::Selection::Channel} else {cursor::Selection::File(state.repo.changed_files.len() - 1,)}}Some(cursor::Selection::File(ix)) => {if 0 == *ix {cursor::Selection::Channel} else {cursor::Selection::File(ix - 1)}}})}Message::CursorSelect(selection) => {state.cursor.selection = Some(selection)}}}fn subs(_state: &State) -> Subscription<Message> {use iced::keyboard::{on_key_press, Key};on_key_press(|key, mods| {if mods.is_empty() {match key {Key::Character(c) => match c.as_str() {"j" => Some(Message::CursorDown),"k" => Some(Message::CursorUp),_ => None,},Key::Named(_) | Key::Unidentified => None,}} else {None}}) - edit in crates/flowers_ui/src/main.rs at line 115
let is_channel_selected = matches!(state.cursor.selection.as_ref(),Some(cursor::Selection::Channel)); - replacement in crates/flowers_ui/src/main.rs at line 122
Element::from(text(channel)),Element::from(button(text(channel)).on_press(Message::CursorSelect(cursor::Selection::Channel)).style(selectable_button_style(is_channel_selected)),), - replacement in crates/flowers_ui/src/main.rs at line 128
let changed_files = Element::from(column(state.repo.changed_files.iter().map(|path| Element::from(text(path))),));let changed_files =Element::from(column(state.repo.changed_files.iter().enumerate().map(|(ix, path)| {let is_selected = matches!(state.cursor.selection.as_ref() ,Some(cursor::Selection::File(selected_ix)) if &ix == selected_ix);Element::from(button(text(path)).on_press(Message::CursorSelect(cursor::Selection::File(ix),)).style(selectable_button_style(is_selected)),)},))); - edit in crates/flowers_ui/src/main.rs at line 150
- edit in crates/flowers_ui/src/main.rs at line 164
}fn selectable_button_style(is_selected: bool,) -> impl Fn(&Theme, button::Status) -> button::Style {move |theme, status| -> button::Style {button::Style {border: Border {color: if is_selected {Color::WHITE} else {Color::TRANSPARENT},width: 1.0,..default()},..button::Catalog::style(theme,&<Theme as button::Catalog>::default(),status,)}}