Skip to content

Commit

Permalink
Update size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
filtsin committed Mar 22, 2023
1 parent 7fbc01b commit 6358ef9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Config {
#[def = "512"]
height: u32,
#[def = "false"]
auto_shrink: bool,
auto_height: bool,
#[def = "false"]
force_window: bool,
window_offsets: Option<(i32, i32)>,
Expand Down
2 changes: 1 addition & 1 deletion src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'a> From<&'a Config> for SurfaceParams {
force_window: config.force_window,
window_offsets: config.window_offsets,
scale: config.scale,
auto_shrink: config.auto_shrink,
auto_height: config.auto_height,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use raqote::{DrawOptions, Path, PathBuilder, Source, StrokeStyle};

pub use background::Params as BgParams;
pub use input_text::Params as InputTextParams;
pub use list_view::{ListItem, Params as ListParams};
pub use list_view::{ListItem, Params as ListParams, ADDITIONAL_CAP};

use crate::{style::Radius, Color};

Expand Down Expand Up @@ -36,7 +36,7 @@ pub enum Widget<'a, It = std::iter::Empty<ListItem<'a>>> {

pub struct ListViewInfo {
pub new_skip: usize,
pub new_height: u32,
pub new_y: u32,
}

impl<'a, It> Widget<'a, It> {
Expand Down
11 changes: 8 additions & 3 deletions src/draw/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::style::Margin;
use crate::{Color, ListViewInfo};
use unicode_segmentation::UnicodeSegmentation;

pub const ADDITIONAL_CAP: u32 = 10;

pub struct Params {
pub font: Font,
pub font_size: u16,
Expand Down Expand Up @@ -100,15 +102,18 @@ where
(self.selected_item - self.skip_offset, self.skip_offset)
};

let mut relative_height = 0;
let mut last_y = point.y as u32
- self.params.font_size as u32
- self.params.margin.bottom as u32
- ADDITIONAL_CAP;

for (i, item) in iter.skip(skip_offset).enumerate().take(displayed_items) {
let relative_offset = (i as f32 + (i > selected_item && has_subname) as i32 as f32)
* (entry_height + item_spacing);
let x_offset = point.x + margin.left;
let y_offset = top_offset + relative_offset;

relative_height = y_offset as u32 + 5;
last_y = y_offset as u32;

let fallback_icon = self
.params
Expand Down Expand Up @@ -203,7 +208,7 @@ where
self.info_channel
.send(ListViewInfo {
new_skip: skip_offset,
new_height: relative_height,
new_y: last_y,
})
.unwrap();

Expand Down
15 changes: 6 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub use color::Color;
pub use desktop::Entry as DesktopEntry;
pub use draw::{DrawTarget, ListViewInfo};

use crate::draw::ADDITIONAL_CAP;

mod animation;
mod color;
mod config;
Expand Down Expand Up @@ -338,16 +340,11 @@ fn draw(
let info: ListViewInfo = rx.recv().unwrap();
state.update_skip_offset(info.new_skip);

if surface.is_shrink() {
let mut full_height = info.new_height
+ list_config.margin.bottom as u32
if surface.is_auto_height() {
let mut full_height = info.new_y
+ list_config.font_size as u32
+ list_config.margin.top as u32;

if info.new_height == 0 {
// Add more space for input if list is empty
full_height += input_config.margin.bottom as u32 + input_config.font_size as u32;
}
+ list_config.margin.bottom as u32
+ ADDITIONAL_CAP;

if animator.contains("HeightAnimation") {
surface.commit();
Expand Down
10 changes: 5 additions & 5 deletions src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Params {
pub window_offsets: Option<(i32, i32)>,
pub scale: Option<u16>,

pub auto_shrink: bool,
pub auto_height: bool,
}

enum RenderSurface {
Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct Surface {
scale: Rc<Cell<u16>>,
dimensions: (u32, u32),

shrink: bool,
auto_height: bool,
}

impl Surface {
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Surface {
pools,
scale,
dimensions: (width, height),
shrink: params.auto_shrink,
auto_height: params.auto_height,
}
}

Expand All @@ -207,8 +207,8 @@ impl Surface {
self.dimensions.1 = height;
}

pub fn is_shrink(&self) -> bool {
self.shrink
pub fn is_auto_height(&self) -> bool {
self.auto_height
}

pub fn get_height(&self) -> u32 {
Expand Down

0 comments on commit 6358ef9

Please sign in to comment.