From 150b0493de001adfe61d8feaadcb5da1dd753a7f Mon Sep 17 00:00:00 2001 From: Menno Finlay-Smits Date: Wed, 6 Feb 2019 02:18:24 +1300 Subject: [PATCH] Add dpi-scale config option (#198) If dpi-scale is set in aminal.toml then this overrides aminal's own DPI calculation. This is useful for working around unusual monitor setups or users just who prefer a different DPI scale. --- README.md | 1 + config/config.go | 17 +++++++++-------- gui/gui.go | 37 +++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8851945..bfe5e8a 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ shell = "/bin/bash" # The shell to run for the terminal session. Default search_url = "https://www.google.com/search?q=$QUERY" # The search engine to use for the "search selected text" action. Defaults to google. Set this to your own search url using $QUERY as the keywords to replace when searching. max_lines = 1000 # Maximum number of lines in the terminal buffer. copy_and_paste_with_mouse = true # Text selected with the mouse is copied to the clipboard on end selection, and is pasted on right mouse button click. +dpi-scale = 0.0 # Override DPI scale. Defaults to 0.0 (let Aminal determine the DPI scale itself). [colours] cursor = "#e8dfd6" diff --git a/config/config.go b/config/config.go index 9bfea1d..890892c 100644 --- a/config/config.go +++ b/config/config.go @@ -7,14 +7,15 @@ import ( ) type Config struct { - DebugMode bool `toml:"debug"` - Slomo bool `toml:"slomo"` - ColourScheme ColourScheme `toml:"colours"` - Shell string `toml:"shell"` - KeyMapping KeyMappingConfig `toml:"keys"` - SearchURL string `toml:"search_url"` - MaxLines uint64 `toml:"max_lines"` - CopyAndPasteWithMouse bool `toml:"copy_and_paste_with_mouse"` + DebugMode bool `toml:"debug"` + Slomo bool `toml:"slomo"` + ColourScheme ColourScheme `toml:"colours"` + DPIScale float32 `toml:"dpi-scale"` + Shell string `toml:"shell"` + KeyMapping KeyMappingConfig `toml:"keys"` + SearchURL string `toml:"search_url"` + MaxLines uint64 `toml:"max_lines"` + CopyAndPasteWithMouse bool `toml:"copy_and_paste_with_mouse"` } type KeyMappingConfig map[string]string diff --git a/gui/gui.go b/gui/gui.go index 7508128..e309423 100644 --- a/gui/gui.go +++ b/gui/gui.go @@ -50,12 +50,12 @@ type GUI struct { arrowCursor *glfw.Cursor defaultCell *buffer.Cell - prevLeftClickX uint16 - prevLeftClickY uint16 - leftClickTime time.Time - leftClickCount int // number of clicks in a serie - single click, double click, or triple click + prevLeftClickX uint16 + prevLeftClickY uint16 + leftClickTime time.Time + leftClickCount int // number of clicks in a serie - single click, double click, or triple click mouseMovedAfterSelectionStarted bool - internalResize bool + internalResize bool } func Min(x, y int) int { @@ -113,23 +113,32 @@ func (g *GUI) GetMonitor() *glfw.Monitor { return currentMonitor } -// RecalculateDpiScale calculates dpi scale in comparison with "standard" monitor's dpi values -func (g *GUI) RecalculateDpiScale() { +// SetDPIScale sets the GUI DPI scale from user configuration (if set) +// or by calculating it from the monitor's configuration and size. +func (g *GUI) SetDPIScale() { + if g.config.DPIScale > 0 { + g.dpiScale = g.config.DPIScale + return + } + g.dpiScale = g.calculateDpiScale() +} + +// calculateDpiScale determines the DPI scale in comparison with "standard" monitor's DPI values. +func (g *GUI) calculateDpiScale() float32 { const standardDpi = 96 const mmPerInch = 25.4 + m := g.GetMonitor() widthMM, _ := m.GetPhysicalSize() - if widthMM == 0 { - g.dpiScale = 1.0 - } else { - monitorDpi := float32(m.GetVideoMode().Width) / (float32(widthMM) / mmPerInch) - g.dpiScale = monitorDpi / standardDpi + return 1.0 } + + monitorDpi := float32(m.GetVideoMode().Width) / (float32(widthMM) / mmPerInch) + return monitorDpi / standardDpi } func New(config *config.Config, terminal *terminal.Terminal, logger *zap.SugaredLogger) (*GUI, error) { - shortcuts, err := config.KeyMapping.GenerateActionMap() if err != nil { return nil, err @@ -293,7 +302,7 @@ func (gui *GUI) Render() error { gui.logger.Debugf("Creating window...") var err error gui.window, err = gui.createWindow() - gui.RecalculateDpiScale() + gui.SetDPIScale() gui.window.SetSize(int(float32(gui.width)*gui.dpiScale), int(float32(gui.height)*gui.dpiScale)) if err != nil {