Skip to content

Commit

Permalink
add window scale option to fix retina displays
Browse files Browse the repository at this point in the history
  • Loading branch information
annacrombie committed Oct 20, 2023
1 parent ee48562 commit 393d29b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
42 changes: 24 additions & 18 deletions cmd/neoray/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Options:
Lists all fonts and writes them to <file>
--nofork
Do not detach process from terminal
--retina
Scale window size for macOS retina display
--version, -v
Prints only the version and quits
--help, -h
Expand All @@ -54,30 +56,32 @@ All other flags forwards to neovim
`

type ParsedArgs struct {
file string
line int
column int
singleInst bool
execPath string
address string
multiGrid bool
nofork bool
others []string
file string
line int
column int
singleInst bool
execPath string
address string
multiGrid bool
nofork bool
others []string
windowScale int
}

// Last boolean value specifies if we should quit after parsing
func ParseArgs(args []string) (ParsedArgs, error, bool) {
// Init defaults
options := ParsedArgs{
file: "",
line: -1,
column: -1,
singleInst: false,
execPath: "nvim",
address: "",
multiGrid: false,
nofork: false,
others: []string{},
file: "",
line: -1,
column: -1,
singleInst: false,
execPath: "nvim",
address: "",
multiGrid: false,
nofork: false,
others: []string{},
windowScale: 1,
}
var err error
for i := 0; i < len(args); i++ {
Expand Down Expand Up @@ -136,6 +140,8 @@ func ParseArgs(args []string) (ParsedArgs, error, bool) {
return options, nil, true
case "--nofork":
options.nofork = true
case "--retina":
options.windowScale = 2
case "--version", "-v":
PrintVersion()
return options, nil, true
Expand Down
8 changes: 4 additions & 4 deletions cmd/neoray/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func InitEditor() {
}
logger.Log(logger.TRACE, "GLFW3 Version:", glfw.GetVersionString())

Editor.window, err = window.New(NAME, 800, 600, bench.IsDebugBuild())
Editor.window, err = window.New(NAME, 800, 600, bench.IsDebugBuild(), Editor.parsedArgs.windowScale)
if err != nil {
logger.Log(logger.FATAL, err)
}
Expand Down Expand Up @@ -335,7 +335,7 @@ func EventHandler(event window.WindowEvent) {
size := Editor.window.Size()
EventHandler(window.WindowEvent{
Type: window.WindowEventResize,
Params: []any{size.Width(), size.Height()},
Params: []any{size.Width() * Editor.parsedArgs.windowScale, size.Height() * Editor.parsedArgs.windowScale},
})
// Only update if tick received
select {
Expand Down Expand Up @@ -364,8 +364,8 @@ func EventHandler(event window.WindowEvent) {
break
}
cellSize := defaultGrid.CellSize()
rows := height / cellSize.Height()
cols := width / cellSize.Width()
rows := (height / Editor.parsedArgs.windowScale) / cellSize.Height()
cols := (width / Editor.parsedArgs.windowScale) / cellSize.Width()
if rows == defaultGrid.rows && cols == defaultGrid.cols {
break
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/neoray/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ func MouseMoveHandler(xpos, ypos float64) {
Editor.window.ShowMouseCursor()
}

inputCache.mousePos.X = int(xpos)
inputCache.mousePos.Y = int(ypos)
inputCache.mousePos.X = int(xpos) * Editor.parsedArgs.windowScale
inputCache.mousePos.Y = int(ypos) * Editor.parsedArgs.windowScale

if Editor.options.contextMenuEnabled {
Editor.contextMenu.MouseMove(inputCache.mousePos)
Expand Down
10 changes: 7 additions & 3 deletions pkg/window/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ type Window struct {
dims common.Rectangle[int] // window dimensions used for restoring window from fullscreen
events WindowEventStack // Cached event stack
eventHandler func(event WindowEvent) // Event handler function will be called for every event at PollEvents call
windowScale int
}

// New creates a window and initializes an opengl context for it
// Im order to use context just call GL function of window
// You must call the Show function to show the window
func New(title string, width, height int, debugContext bool) (*Window, error) {
func New(title string, width, height int, debugContext bool, windowScale int) (*Window, error) {
if width <= 0 || height <= 0 {
return nil, errors.New("Window dimensions must bigger than zero")
}

window := new(Window)

window.windowScale = windowScale

// Set opengl library version
// TODO: make it 2.1 (needs some research)
glfw.WindowHint(glfw.ContextVersionMajor, 3)
Expand Down Expand Up @@ -73,6 +76,7 @@ func New(title string, width, height int, debugContext bool) (*Window, error) {

window.handle.SetFramebufferSizeCallback(func(w *glfw.Window, width, height int) {
window.events.Push(WindowEventResize, width, height)
window.events.Push(WindowEventResize, width*windowScale, height*windowScale)
})

window.handle.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
Expand Down Expand Up @@ -139,7 +143,7 @@ func (window *Window) Resize(size common.Vector2[int]) {
if size.Y <= 0 {
size.Y = window.Size().Height()
}
window.handle.SetSize(size.X, size.Y)
window.handle.SetSize(size.X*window.windowScale, size.Y*window.windowScale)
}

func (window *Window) SetMinSize(minSize common.Vector2[int]) {
Expand All @@ -154,7 +158,7 @@ func (window *Window) Dimensions() common.Rectangle[int] {

func (window *Window) Size() common.Vector2[int] {
W, H := window.handle.GetSize()
return common.Vector2[int]{X: W, Y: H}
return common.Vector2[int]{X: W * window.windowScale, Y: H * window.windowScale}
}

func (window *Window) Viewport() common.Rectangle[int] {
Expand Down

0 comments on commit 393d29b

Please sign in to comment.