04b1829a0b160e643abdd6fbd6144f94715db231 — Phil Hagelberg 2 months ago 869b005
Allow setting fixed dimensions.
3 files changed, 53 insertions(+), 21 deletions(-)

M polywell/commands.fnl
M polywell/frontend/love.lua
M polywell/old.lua
M polywell/commands.fnl => polywell/commands.fnl +13 -3
@@ 83,10 83,12 @@ If it's already open, switch to the existing buffer."
     (set state.window 1))
   (set state.b (. state.windows state.window 2)))
 
+(var last-split nil)
+
 (fn split [style]
   "Split the screen.
 Takes a style arg which can be :vertical, :horizontal, :triple, or nil."
-  (let [(w h) (frontend.get_wh)
+  (let [(w h) (editor.get-wh)
         hw (/ w 2) hh (/ h 2)
         second (or state.last-buffer state.b)]
     (set state.windows (if (= style :triple)


@@ 101,6 103,7 @@ Takes a style arg which can be :vertical, :horizontal, :triple, or nil."
                             [[10 (- hh 10) w h] second]]
                            :else
                            [[[10 10 (- w 10) (- h 10)] state.b]]))
+    (set last-split style)
     (set state.window 1)))
 
 (fn reload []


@@ 318,7 321,7 @@ If the mode doesn't provide an indenter, indent to match the previous line."
 
 (fn window-lines []
   (let [[_ _ _ h] (. state.windows state.window 1)]
-    (math.floor (- (/ h (frontend.line-height)) 1))))
+    (math.floor (- (/ (or h 0) (frontend.line-height)) 1))))
 
 (fn scroll [n]
   (set state.b.point_line (-> (+ state.b.point_line (* (window-lines) (or n 1)))


@@ 338,6 341,13 @@ If the mode doesn't provide an indenter, indent to match the previous line."
                             (editor.echo "Command not found: " cmd-name))))
                     {:completer (partial completion.for editor.cmd)}))
 
+(fn toggle-fullscreen []
+  (frontend.toggle_fullscreen)
+  ;; recalculate screen split sizes
+  ;; TODO: bug; when disabling full-screen with a split active, it makes the new
+  ;; split too small
+  (split last-split))
+
 ;; undo
 
 {:find-file find-file :close close :search search :replace replace


@@ 346,7 356,7 @@ If the mode doesn't provide an indenter, indent to match the previous line."
  :focus-next focus-next :split split
  :save save :reload reload :revert revert :complete complete
  :go-to-line go-to-line :execute execute
- :toggle-fullscreen frontend.toggle_fullscreen :scale frontend.scale
+ :toggle-fullscreen toggle-fullscreen :scale frontend.scale
  :history-prev history-prev :history-next history-next
  :indent indent :newline-and-indent #(do (editor.cmd.newline) (indent))
  :scroll-up (partial scroll -1) :scroll-down scroll

M polywell/frontend/love.lua => polywell/frontend/love.lua +38 -18
@@ 5,11 5,26 @@ local lume = require("polywell.lib.lume")
 local row_height, scroll_rows, em, w, h
 local padding, buffer_padding, offset = 10, 0, 0
 local _, lfs = pcall(require, "lfs")
-local canvas
+local canvas, fixed_w, fixed_h
 local scale = 1
 
 local exists = love.filesystem.getInfo or love.filesystem.exists -- 0.10.x
 
+local reset_canvas = function()
+   love.graphics.setCanvas()
+   local rw, rh = love.graphics.getDimensions()
+   if fixed_w and fixed_h then
+      w, h = fixed_w, fixed_h
+      scale = math.min(rw/w,rh/h)
+      canvas = love.graphics.newCanvas(rw, rh)
+      canvas:setFilter("nearest", "nearest")
+   else
+      canvas = love.graphics.newCanvas(rw, rh)
+      canvas:setFilter("nearest", "nearest")
+      w, h = rw/scale, rh/scale
+   end
+end
+
 local render_line = function(ln2, y)
    if(ln2 == "\f\n" or ln2 == "\f") then
       love.graphics.line(0, y + 0.5 * row_height, w, y + 0.5 * row_height)


@@ 156,12 171,7 @@ local draw = function(b, buffers_where, echo_message, colors, get_prop)
 end
 
 local wrap = function(f, ...)
-   local rw, rh = love.graphics.getDimensions()
-   if(not canvas) then
-      canvas = love.graphics.newCanvas(rw, rh)
-      canvas:setFilter("nearest", "nearest")
-   end
-   w, h = rw/scale, rh/scale
+   if(not canvas) then reset_canvas() end
    love.graphics.setCanvas({canvas, stencil=true})
    love.graphics.clear()
    love.graphics.setColor(normalize_color({255, 255, 255}))


@@ 179,6 189,8 @@ local resolve = function(path)
    end
 end
 
+local get_wh = function() return canvas:getDimensions() end
+
 return {
    write = function(path, contents)
       if not contents then return end


@@ 224,19 236,24 @@ return {
       if love.window then love.system.setClipboardText(contents) end
    end,
 
-   get_wh = function()
+   get_wh = get_wh,
+
+   get_buffer_wh = function()
       local _,_,sw,sh = love.graphics.getScissor()
-      if(not sw and not sh) then
-         local ww,wh = love.graphics.getDimensions()
-         return ww/scale, wh/scale
-      end
-      return sw, sh
+      if(sw and sh) then return sw, sh end
+      return get_wh()
+   end,
+
+   set_wh = function(nw, nh)
+      fixed_w, fixed_h = nw, nh
+      reset_canvas()
    end,
 
-   set_scale = function(s) scale = s end,
+   set_scale = function(s) scale = s reset_canvas() end,
    scale = function(s)
       s = s or 1
-      scale, canvas = math.max(1, math.min(scale+s, 4)), nil
+      scale = math.max(1, math.min(scale+s, 4))
+      reset_canvas()
    end,
 
    toggle_fullscreen = function()


@@ 247,6 264,8 @@ return {
          w, h = tonumber(new_w), tonumber(new_h)
          love.window.setMode(w, h)
          love.filesystem.remove("fullscreen")
+         reset_canvas()
+         return false
       else
          love.filesystem.write("window", w .. " " .. h)
          love.filesystem.write("fullscreen", "true")


@@ 254,6 273,8 @@ return {
          love.window.setMode(dw, dh, {fullscreen=true,
                                       fullscreentype="desktop",
                                       resizable=false})
+         reset_canvas()
+         return true
       end
    end,
 


@@ 275,8 296,7 @@ return {
 
    quit = love.event.quit,
 
-   ["line-height"] = function() return row_height end,
+   ["line-height"] = function() return row_height or 1 end,
 
-   -- blowing away the canvas will cause it to get recreated with the right size
-   resize = function() canvas = nil end,
+   resize = function() reset_canvas() end,
 }

M polywell/old.lua => polywell/old.lua +2 -0
@@ 1013,7 1013,9 @@ return {
       return f
    end,
 
+   ["set-wh"] = frontend.set_wh,
    ["get-wh"] = frontend.get_wh,
+   ["get-buffer-wh"] = frontend.get_buffer_wh,
    ["set-scale"] = frontend.set_scale,
 
    ["current-split"] = function() return state.window end,