Ensure redraws are done when last image is lost

This commit is contained in:
Jeremy Soller 2023-11-21 09:37:03 -07:00
parent 7782a421a3
commit 3fc8a76509
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
2 changed files with 24 additions and 32 deletions

View file

@ -398,11 +398,7 @@ impl App {
self.update_nav_bar_active(); self.update_nav_bar_active();
let title = match self.active_tab() { let title = match self.active_tab() {
Some(tab) => { Some(tab) => tab.title(),
// Hack to ensure redraw on changing tabs
tab.editor.lock().unwrap().buffer_mut().set_redraw(true);
tab.title()
}
None => format!("No Open File"), None => format!("No Open File"),
}; };
@ -836,8 +832,6 @@ impl Application for App {
Some(tab) => { Some(tab) => {
// Close context menu // Close context menu
tab.context_menu = None; tab.context_menu = None;
// Hack to ensure editor redraws
tab.editor.lock().unwrap().buffer_mut().set_redraw(true);
// Run action's message // Run action's message
return self.update(action.message()); return self.update(action.message());
} }
@ -849,8 +843,6 @@ impl Application for App {
Some(tab) => { Some(tab) => {
// Update context menu // Update context menu
tab.context_menu = position_opt; tab.context_menu = position_opt;
// Hack to ensure editor redraws
tab.editor.lock().unwrap().buffer_mut().set_redraw(true);
} }
None => {} None => {}
} }
@ -874,10 +866,6 @@ impl Application for App {
self.core.window.show_context = true; self.core.window.show_context = true;
} }
self.set_context_title(context_page.title()); 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 => { Message::ToggleWordWrap => {
self.config.word_wrap = !self.config.word_wrap; self.config.word_wrap = !self.config.word_wrap;

View file

@ -328,7 +328,8 @@ where
// Shape and layout as needed // Shape and layout as needed
editor.shape_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 // Draw to pixel buffer
let mut pixels = vec![0; image_w as usize * image_h as usize * 4]; let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
{ {
@ -378,26 +379,30 @@ where
editor.buffer_mut().set_redraw(false); editor.buffer_mut().set_redraw(false);
state.scale_factor.set(scale_factor); state.scale_factor.set(scale_factor);
*state.handle.lock().unwrap() = *handle_opt = Some(image::Handle::from_pixels(
image::Handle::from_pixels(image_w as u32, image_h as u32, pixels); image_w as u32,
image_h as u32,
pixels,
));
} }
let handle = state.handle.lock().unwrap().clone();
let image_position = let image_position =
layout.position() + [self.padding.left as f32, self.padding.top as f32].into(); layout.position() + [self.padding.left as f32, self.padding.top as f32].into();
let image_size = image::Renderer::dimensions(renderer, &handle); if let Some(ref handle) = *handle_opt {
image::Renderer::draw( let image_size = image::Renderer::dimensions(renderer, &handle);
renderer, image::Renderer::draw(
handle, renderer,
Rectangle::new( handle.clone(),
image_position, Rectangle::new(
Size::new( image_position,
image_size.width as f32 / scale_factor, Size::new(
image_size.height as f32 / scale_factor, image_size.width as f32 / scale_factor,
image_size.height as f32 / scale_factor,
),
), ),
), [0.0; 4],
[0.0; 4], );
); }
// Draw scrollbar // Draw scrollbar
let scrollbar_alpha = match &state.dragging { let scrollbar_alpha = match &state.dragging {
@ -661,7 +666,7 @@ pub struct State {
scale_factor: Cell<f32>, scale_factor: Cell<f32>,
scroll_pixels: f32, scroll_pixels: f32,
scrollbar_rect: Cell<Rectangle<f32>>, scrollbar_rect: Cell<Rectangle<f32>>,
handle: Mutex<image::Handle>, handle_opt: Mutex<Option<image::Handle>>,
} }
impl State { impl State {
@ -673,8 +678,7 @@ impl State {
scale_factor: Cell::new(1.0), scale_factor: Cell::new(1.0),
scroll_pixels: 0.0, scroll_pixels: 0.0,
scrollbar_rect: Cell::new(Rectangle::default()), scrollbar_rect: Cell::new(Rectangle::default()),
//TODO: make option! handle_opt: Mutex::new(None),
handle: Mutex::new(image::Handle::from_pixels(1, 1, vec![0, 0, 0, 0])),
} }
} }
} }