From 091d0e910df95831c240d119cc3b923593a3d62f Mon Sep 17 00:00:00 2001 From: Piotr Grabowski Date: Sat, 16 Jul 2022 00:49:56 +0200 Subject: [PATCH] feat(zoom): add FitToWidthZoom mode BREAKING CHANGE: ZoomMode config key type got changed, the program will not start with an old config file --- config.go | 26 +++++++++++++++++++------- gomicsv.ui | 10 ++++++++++ image.go | 11 ++++++++--- menu.go | 15 +++++++++++---- ui.go | 8 +++++--- widgets.go | 1 + 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/config.go b/config.go index 389e317..0c3cedc 100644 --- a/config.go +++ b/config.go @@ -36,7 +36,7 @@ const ( ) type Config struct { - ZoomMode string + ZoomMode ZoomMode Enlarge bool Shrink bool LastDirectory string @@ -95,7 +95,7 @@ func (app *App) saveConfig() { } func (c *Config) setDefaults() { - c.ZoomMode = "FitToWidth" + c.ZoomMode = FitToWidth c.Shrink = true c.Enlarge = true c.WindowWidth = 640 @@ -152,17 +152,29 @@ func (c *Config) save(path string) error { return err } -func (app *App) setZoomMode(mode string) { +type ZoomMode int + +const ( + FitToWidth ZoomMode = iota + FitToHalfWidth + FitToHeight + BestFit + Original +) + +func (app *App) setZoomMode(mode ZoomMode) { switch mode { - case "FitToWidth": + case FitToWidth: app.W.MenuItemFitToWidth.SetActive(true) - case "FitToHeight": + case FitToHalfWidth: + app.W.MenuItemFitToHalfWidth.SetActive(true) + case FitToHeight: app.W.MenuItemFitToHeight.SetActive(true) - case "BestFit": + case BestFit: app.W.MenuItemBestFit.SetActive(true) default: app.W.MenuItemOriginal.SetActive(true) - mode = "Original" + mode = Original } app.Config.ZoomMode = mode diff --git a/gomicsv.ui b/gomicsv.ui index 3971b0c..83fcadf 100644 --- a/gomicsv.ui +++ b/gomicsv.ui @@ -217,6 +217,16 @@ MenuItemBestFit + + + true + false + Fit to half-width + true + true + MenuItemBestFit + + true diff --git a/image.go b/image.go index 41475bc..557eeec 100644 --- a/image.go +++ b/image.go @@ -117,14 +117,19 @@ func (app *App) getScaledSize() (scale float64) { w, h := app.pixbufSize() switch app.Config.ZoomMode { - case "FitToWidth": + case FitToWidth: needscale := (app.Config.Enlarge && w < scrw) || (app.Config.Shrink && w > scrw) if needscale { return float64(scrw) / float64(w) } - case "FitToHeight": + case FitToHalfWidth: + needscale := (app.Config.Enlarge && w/2 < scrw) || (app.Config.Shrink && w/2 > scrw) + if needscale { + return float64(scrw) / float64(w/2) + } + case FitToHeight: return float64(scrh) / float64(h) - case "BestFit": + case BestFit: needscale := (app.Config.Enlarge && (w < scrw && h < scrh)) || (app.Config.Shrink && (w > scrw || h > scrh)) if needscale { fw, _ := util.Fit(w, h, scrw, scrh) diff --git a/menu.go b/menu.go index 3bdb431..35c6ffc 100644 --- a/menu.go +++ b/menu.go @@ -89,25 +89,31 @@ func (app *App) menuInit() { app.W.MenuItemOriginal.Connect("toggled", func() { if app.W.MenuItemOriginal.GetActive() { - app.setZoomMode("Original") + app.setZoomMode(Original) } }) app.W.MenuItemBestFit.Connect("toggled", func() { if app.W.MenuItemBestFit.GetActive() { - app.setZoomMode("BestFit") + app.setZoomMode(BestFit) } }) app.W.MenuItemFitToWidth.Connect("toggled", func() { if app.W.MenuItemFitToWidth.GetActive() { - app.setZoomMode("FitToWidth") + app.setZoomMode(FitToWidth) + } + }) + + app.W.MenuItemFitToHalfWidth.Connect("toggled", func() { + if app.W.MenuItemFitToHalfWidth.GetActive() { + app.setZoomMode(FitToHalfWidth) } }) app.W.MenuItemFitToHeight.Connect("toggled", func() { if app.W.MenuItemFitToHeight.GetActive() { - app.setZoomMode("FitToHeight") + app.setZoomMode(FitToHeight) } }) @@ -290,6 +296,7 @@ func (app *App) menuSetupAccels() { {&app.W.MenuItemBestFit.MenuItem, Accel{gdk.KEY_B, 0}}, {&app.W.MenuItemOriginal.MenuItem, Accel{gdk.KEY_O, 0}}, {&app.W.MenuItemFitToWidth.MenuItem, Accel{gdk.KEY_W, 0}}, + {&app.W.MenuItemFitToHalfWidth.MenuItem, Accel{gdk.KEY_W, gdk.MOD1_MASK}}, {&app.W.MenuItemFitToHeight.MenuItem, Accel{gdk.KEY_H, 0}}, {&app.W.MenuItemFullscreen.MenuItem, Accel{gdk.KEY_F, 0}}, {&app.W.MenuItemRandom.MenuItem, Accel{gdk.KEY_R, 0}}, diff --git a/ui.go b/ui.go index 4d5e929..8737df2 100644 --- a/ui.go +++ b/ui.go @@ -92,11 +92,13 @@ func (app *App) syncWidgetsToConfig() { app.W.MenuItemMangaMode.SetActive(app.Config.MangaMode) switch app.Config.ZoomMode { - case "FitToWidth": + case FitToWidth: app.W.MenuItemFitToWidth.SetActive(true) - case "FitToHeight": + case FitToHalfWidth: + app.W.MenuItemFitToHalfWidth.SetActive(true) + case FitToHeight: app.W.MenuItemFitToHeight.SetActive(true) - case "BestFit": + case BestFit: app.W.MenuItemBestFit.SetActive(true) default: app.W.MenuItemOriginal.SetActive(true) diff --git a/widgets.go b/widgets.go index 0c8b558..047f41f 100644 --- a/widgets.go +++ b/widgets.go @@ -89,6 +89,7 @@ type Widgets struct { MenuItemBestFit *gtk.RadioMenuItem `build:"MenuItemBestFit"` MenuItemOriginal *gtk.RadioMenuItem `build:"MenuItemOriginal"` MenuItemFitToWidth *gtk.RadioMenuItem `build:"MenuItemFitToWidth"` + MenuItemFitToHalfWidth *gtk.RadioMenuItem `build:"MenuItemFitToHalfWidth"` MenuItemFitToHeight *gtk.RadioMenuItem `build:"MenuItemFitToHeight"` MenuItemAbout *gtk.MenuItem `build:"MenuItemAbout"` GoToThumbnailImage *gtk.Image `build:"GoToThumbnailImage"`