Skip to content

Commit

Permalink
Add new parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
filtsin committed Mar 22, 2023
1 parent 6358ef9 commit eb13330
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct Config {
#[def = "false"]
auto_height: bool,
#[def = "false"]
animations: bool,
#[def = "false"]
force_window: bool,
window_offsets: Option<(i32, i32)>,
scale: Option<u16>,
Expand All @@ -56,6 +58,10 @@ impl Config {
self.icon = None;
}

pub fn animations_enabled(&self) -> bool {
self.animations
}

pub fn set_prompt(&mut self, prompt: String) {
self.input_text.prompt = Some(prompt);
}
Expand Down Expand Up @@ -101,6 +107,8 @@ struct ListItems {
item_spacing: f32,
#[def = "10.0"]
icon_spacing: f32,
#[def = "true"]
show_default: bool,
}

#[derive(Defaults, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl<'a> From<&'a Config> for ListParams {
action_left_margin: config.list_items.action_left_margin,
item_spacing: config.list_items.item_spacing,
icon_spacing: config.list_items.icon_spacing,
show_default: config.list_items.show_default,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/draw/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Params {
pub action_left_margin: f32,
pub item_spacing: f32,
pub icon_spacing: f32,
pub show_default: bool,
}

pub struct ListItem<'a> {
Expand Down
36 changes: 26 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ fn main_inner() {
let background_config = config.param();
let input_config = config.param();
let list_config = config.param();
let animations = config.animations_enabled();

if !env.get_shell().unwrap().needs_configure() {
draw(
Expand All @@ -227,6 +228,7 @@ fn main_inner() {
&input_config,
&list_config,
&mut surface,
animations,
);
}

Expand All @@ -237,7 +239,9 @@ fn main_inner() {
loop {
let mut should_redraw = false;
for event in key_stream.try_iter() {
animator.cancel_animation("HeightAnimation");
if animations {
animator.cancel_animation("HeightAnimation");
}

should_redraw = true;

Expand All @@ -264,6 +268,7 @@ fn main_inner() {
&input_config,
&list_config,
&mut surface,
animations,
);
}

Expand Down Expand Up @@ -307,10 +312,11 @@ fn draw(
input_config: &draw::InputTextParams,
list_config: &draw::ListParams,
surface: &mut surface::Surface,
animations: bool,
) {
use std::iter::once;

state.process_entries();
state.process_entries(list_config.show_default);

let (tx, rx) = oneshot::channel();

Expand Down Expand Up @@ -356,16 +362,26 @@ fn draw(
full_height = background_config.height;
}

animator.add_animation(
"HeightAnimation".into(),
old_height as f64,
full_height as f64,
Duration::from_millis(500),
animation::AnimationType::Single,
);
// Hack for input_changed: If input buffer capacity is 0 then we do not typing
// anything, so disable animation for first calls of draw (for support
// ListItem::show_default)
if animations && state.input_changed() {
animator.add_animation(
"HeightAnimation".into(),
old_height as f64,
full_height as f64,
Duration::from_millis(500),
animation::AnimationType::Single,
);
}
}

if animations && state.input_changed() {
surface.update_height(old_height);
} else {
surface.update_height(full_height);
}

surface.update_height(old_height);
surface.commit();
} else {
surface.commit();
Expand Down
13 changes: 11 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ impl State {
self.input_buffer.raw_input()
}

pub fn input_changed(&self) -> bool {
self.input_buffer.raw_input.capacity() != 0
}

pub fn skip_offset(&self) -> usize {
self.skip_offset
}
Expand All @@ -244,9 +248,14 @@ impl State {
.list_items(&self.inner, self.selected_item, self.selected_subitem)
}

pub fn process_entries(&mut self) {
pub fn process_entries(&mut self, show_default: bool) {
self.filtered_lines = if self.input_buffer.search_string().is_empty() {
FilteredLines::unfiltred(self.inner.entries_len())
let len = if show_default {
self.inner.entries_len()
} else {
0
};
FilteredLines::unfiltred(len)
} else {
FilteredLines::searched(self.inner.text_entries(), self.input_buffer.search_string())
};
Expand Down

0 comments on commit eb13330

Please sign in to comment.