Skip to content

Commit

Permalink
Ensure redraws are done when last image is lost
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Nov 21, 2023
1 parent 7782a42 commit 3fc8a76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
14 changes: 1 addition & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,7 @@ impl App {
self.update_nav_bar_active();

let title = match self.active_tab() {
Some(tab) => {
// Hack to ensure redraw on changing tabs
tab.editor.lock().unwrap().buffer_mut().set_redraw(true);
tab.title()
}
Some(tab) => tab.title(),
None => format!("No Open File"),
};

Expand Down Expand Up @@ -836,8 +832,6 @@ impl Application for App {
Some(tab) => {
// Close context menu
tab.context_menu = None;
// Hack to ensure editor redraws
tab.editor.lock().unwrap().buffer_mut().set_redraw(true);
// Run action's message
return self.update(action.message());
}
Expand All @@ -849,8 +843,6 @@ impl Application for App {
Some(tab) => {
// Update context menu
tab.context_menu = position_opt;
// Hack to ensure editor redraws
tab.editor.lock().unwrap().buffer_mut().set_redraw(true);
}
None => {}
}
Expand All @@ -874,10 +866,6 @@ impl Application for App {
self.core.window.show_context = true;
}
self.set_context_title(context_page.title());

// Hack to ensure tab redraws.
//TODO: tab does not redraw when using Close button!
return self.update_tab();
}
Message::ToggleWordWrap => {
self.config.word_wrap = !self.config.word_wrap;
Expand Down
42 changes: 23 additions & 19 deletions src/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ where
// Shape and layout as needed
editor.shape_as_needed();

if editor.buffer().redraw() {
let mut handle_opt = state.handle_opt.lock().unwrap();
if editor.buffer().redraw() || handle_opt.is_none() {
// Draw to pixel buffer
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
{
Expand Down Expand Up @@ -378,26 +379,30 @@ where
editor.buffer_mut().set_redraw(false);

state.scale_factor.set(scale_factor);
*state.handle.lock().unwrap() =
image::Handle::from_pixels(image_w as u32, image_h as u32, pixels);
*handle_opt = Some(image::Handle::from_pixels(
image_w as u32,
image_h as u32,
pixels,
));
}

let handle = state.handle.lock().unwrap().clone();
let image_position =
layout.position() + [self.padding.left as f32, self.padding.top as f32].into();
let image_size = image::Renderer::dimensions(renderer, &handle);
image::Renderer::draw(
renderer,
handle,
Rectangle::new(
image_position,
Size::new(
image_size.width as f32 / scale_factor,
image_size.height as f32 / scale_factor,
if let Some(ref handle) = *handle_opt {
let image_size = image::Renderer::dimensions(renderer, &handle);
image::Renderer::draw(
renderer,
handle.clone(),
Rectangle::new(
image_position,
Size::new(
image_size.width as f32 / scale_factor,
image_size.height as f32 / scale_factor,
),
),
),
[0.0; 4],
);
[0.0; 4],
);
}

// Draw scrollbar
let scrollbar_alpha = match &state.dragging {
Expand Down Expand Up @@ -661,7 +666,7 @@ pub struct State {
scale_factor: Cell<f32>,
scroll_pixels: f32,
scrollbar_rect: Cell<Rectangle<f32>>,
handle: Mutex<image::Handle>,
handle_opt: Mutex<Option<image::Handle>>,
}

impl State {
Expand All @@ -673,8 +678,7 @@ impl State {
scale_factor: Cell::new(1.0),
scroll_pixels: 0.0,
scrollbar_rect: Cell::new(Rectangle::default()),
//TODO: make option!
handle: Mutex::new(image::Handle::from_pixels(1, 1, vec![0, 0, 0, 0])),
handle_opt: Mutex::new(None),
}
}
}

0 comments on commit 3fc8a76

Please sign in to comment.