Skip to content

Commit

Permalink
demo: hit testing the titlebar only in normal state and if not hoveri…
Browse files Browse the repository at this point in the history
…ng over the resize grips already
  • Loading branch information
capr committed Sep 25, 2015
1 parent afe66ec commit 95e89f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
15 changes: 7 additions & 8 deletions nw.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ Create a window (fields of _`t`_ below with default value in parenthesis):
* `title` - title ('')
* `transparent` - transparent window (false)
* `corner_radius` - rounded corners (0)
* `resize_grip` - resize grip width for frame='none' windows (8)
* __behavior__
* `parent` - parent window
* `sticky` - moves with parent (false)
Expand Down Expand Up @@ -847,14 +846,14 @@ __NOTE:__ This event does not fire in Linux.
Event: window was moved/resized. These events also fire when a window is
hidden or minimized in which case all args are nil, so make sure to test for that.

### `win:hittest(x, y) -> where`
### `win:hittest(x, y, where) -> where`

Hit test for moving and resizing frameless windows. Given a set of
window-relative coordinates, return 'left', 'top', 'right', 'bottom',
'topleft', 'bottomright', 'topright' or 'bottomleft' to specify that the
window should be resized, 'move' which means the window should be moved,
false which means the coordinates are over the client area, or nil
which means that standard resizing based on `resize_grip` should take place.
Hit test for moving and resizing frameless windows. Return 'left', 'top',
'right', 'bottom', 'topleft', 'bottomright', 'topright' or 'bottomleft'
to specify that the window should be resized, 'move' which means the window
should be moved, false which means the coordinates are over the client area,
or nil which means that standard resizing should take place. The `where`
arg is the default response for the given coordinates.

## Size constraints

Expand Down
34 changes: 24 additions & 10 deletions nw_demo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ if not ... then
local dsp = app:main_display()
local win = app:window{x = dsp.w - 900, y = 200, cw = 800, ch = 400,
min_cw = 200, min_ch = 28,
frame = 'none', transparent = false, corner_radius = 4, topmost = true, visible = false}
frame = 'none',
transparent = true,
corner_radius = 0,
topmost = false,
visible = false}

local function reload()
package.loaded.nw_demo = nil
local ok, methods = pcall(require, 'nw_demo')
Expand Down Expand Up @@ -282,14 +287,10 @@ function win:repaint()
end

function win:mousemove(mx, my)
local w, h = self:client_size()
set_dims(w, h)

self.min_hover = box2d.hit(mx, my, unpack(min_rect))
self.max_hover = box2d.hit(mx, my, unpack(max_rect))
self.close_hover = box2d.hit(mx, my, unpack(close_rect))
self.titlebar_hover = box2d.hit(mx, my, unpack(titlebar_rect))
self:invalidate()
end

function win:mouseleave()
self:invalidate()
end

Expand All @@ -305,9 +306,22 @@ function win:deactivated()
self:invalidate()
end

function win:hittest(x, y, where_resize)
function win:hittest(mx, my, where)

local w, h = self:client_size()
set_dims(w, h)

self.min_hover = box2d.hit(mx, my, unpack(min_rect))
self.max_hover = box2d.hit(mx, my, unpack(max_rect))
self.close_hover = box2d.hit(mx, my, unpack(close_rect))
self.titlebar_hover =
not where --the titlebar is below the invisible resize grips
and not self:ismaximized()
and not self:fullscreen()
and box2d.hit(mx, my, unpack(titlebar_rect))

if self.min_hover or self.max_hover or self.close_hover then return false end
if not where_resize and self.titlebar_hover then return 'move' end
if self.titlebar_hover then return 'move' end
end

function win:mouseup(x, y)
Expand Down

0 comments on commit 95e89f6

Please sign in to comment.