use saudade::{Event, EventCtx, Painter, PopupRequest, Rect, Theme, Widget};
use crate::backend::Diff;
use crate::imagediff::ImageComparison;
use crate::widgets::{DiffMode, DiffView, ImageDiffView};
pub struct DiffPane {
text: DiffView,
image: ImageDiffView,
showing_image: bool,
focused: bool,
}
impl DiffPane {
pub fn new(rect: Rect) -> Self {
Self {
text: DiffView::new(rect),
image: ImageDiffView::new(rect),
showing_image: false,
focused: false,
}
}
pub fn with_font_size(mut self, size: f32) -> Self {
self.text = self.text.with_font_size(size);
self.image = self.image.with_font_size(size);
self
}
pub fn set_diff(&mut self, diff: Diff) {
self.text.set_diff(diff);
self.set_showing_image(false);
}
pub fn show_image(&mut self, comparison: ImageComparison) {
self.image.set_comparison(Some(comparison));
self.set_showing_image(true);
}
pub fn showing_image(&self) -> bool {
self.showing_image
}
pub fn cycle_image_mode(&mut self) {
if self.showing_image {
self.image.cycle_mode();
}
}
pub fn show_image_side(&mut self, before: bool) {
if self.showing_image {
self.image.show_side(before);
}
}
pub fn set_mode(&mut self, mode: DiffMode) {
self.text.set_mode(mode);
}
pub fn take_action(&mut self) -> Option<(usize, usize)> {
if self.showing_image {
None
} else {
self.text.take_action()
}
}
pub fn is_empty(&self) -> bool {
if self.showing_image {
self.image.is_empty()
} else {
self.text.is_empty()
}
}
fn set_showing_image(&mut self, showing_image: bool) {
if showing_image == self.showing_image {
return;
}
self.showing_image = showing_image;
if self.focused {
self.text.set_focused(!showing_image);
self.image.set_focused(showing_image);
}
}
fn active(&self) -> &dyn Widget {
if self.showing_image {
&self.image
} else {
&self.text
}
}
fn active_mut(&mut self) -> &mut dyn Widget {
if self.showing_image {
&mut self.image
} else {
&mut self.text
}
}
}
impl Widget for DiffPane {
fn bounds(&self) -> Rect {
self.active().bounds()
}
fn paint(&mut self, painter: &mut Painter, theme: &Theme) {
self.active_mut().paint(painter, theme);
}
fn event(&mut self, event: &Event, ctx: &mut EventCtx) {
self.active_mut().event(event, ctx);
}
fn captures_pointer(&self) -> bool {
self.active().captures_pointer()
}
fn focusable(&self) -> bool {
self.active().focusable()
}
fn set_focused(&mut self, focused: bool) {
self.focused = focused;
self.active_mut().set_focused(focused);
}
fn focus_first(&mut self) -> bool {
self.focused = true;
self.active_mut().focus_first()
}
fn layout(&mut self, bounds: Rect) {
self.text.layout(bounds);
self.image.layout(bounds);
}
fn wants_ticks(&self) -> bool {
self.active().wants_ticks()
}
fn popup_request(&self) -> Option<PopupRequest> {
self.active().popup_request()
}
}