Actually track clicks properly in buttons in calculator

This commit is contained in:
Kevin Lange 2017-01-09 14:24:18 +09:00
parent 71471f1c74
commit a2ce60bd40

View File

@ -56,45 +56,55 @@ def draw_button(ctx,x,y,w,h,hilight):
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
ctx.set_source_rgba(44/255,71/255,91/255,29/255)
ctx.set_line_width(4)
ctx.stroke()
if hilight == 2:
rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
ctx.set_source_rgba(134/255,173/255,201/255,1.0)
ctx.set_line_width(2)
ctx.stroke()
rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
ctx.set_source_rgba(158/255,169/255,177/255,1.0)
ctx.set_line_width(2)
ctx.stroke()
if hilight:
pat = cairo.LinearGradient(2+x,2+y,2+x,2+y+h-4)
pat.add_color_stop_rgba(0,1,1,1,1)
pat.add_color_stop_rgba(1,229/255,229/255,246/255,1)
rounded_rectangle(ctx,2+x,2+y,w-4,h-4,2.0)
ctx.set_source(pat)
rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
ctx.set_source_rgba(202/255,211/255,232/255,1.0)
ctx.fill()
pat = cairo.LinearGradient(3+x,3+y,3+x,3+y+h-4)
pat.add_color_stop_rgba(0,252/255,252/255,254/255,1)
pat.add_color_stop_rgba(1,212/255,223/255,251/255,1)
rounded_rectangle(ctx,3+x,3+y,w-5,h-5,2.0)
ctx.set_source(pat)
ctx.fill()
else:
pat = cairo.LinearGradient(2+x,2+y,2+x,2+y+h-4)
pat.add_color_stop_rgba(0,1,1,1,1)
pat.add_color_stop_rgba(1,241/255,241/255,244/255,1)
rounded_rectangle(ctx,2+x,2+y,w-4,h-4,2.0)
ctx.set_source(pat)
ctx.fill()
rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
ctx.set_source_rgba(44/255,71/255,91/255,29/255)
ctx.set_line_width(4)
ctx.stroke()
pat = cairo.LinearGradient(3+x,3+y,3+x,3+y+h-4)
pat.add_color_stop_rgba(0,252/255,252/255,254/255,1)
pat.add_color_stop_rgba(1,223/255,225/255,230/255,1)
rounded_rectangle(ctx,3+x,3+y,w-5,h-5,2.0)
ctx.set_source(pat)
ctx.fill()
rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
ctx.set_source_rgba(158/255,169/255,177/255,1.0)
ctx.set_line_width(2)
ctx.stroke()
if hilight == 1:
pat = cairo.LinearGradient(2+x,2+y,2+x,2+y+h-4)
pat.add_color_stop_rgba(0,1,1,1,1)
pat.add_color_stop_rgba(1,229/255,229/255,246/255,1)
rounded_rectangle(ctx,2+x,2+y,w-4,h-4,2.0)
ctx.set_source(pat)
ctx.fill()
pat = cairo.LinearGradient(3+x,3+y,3+x,3+y+h-4)
pat.add_color_stop_rgba(0,252/255,252/255,254/255,1)
pat.add_color_stop_rgba(1,212/255,223/255,251/255,1)
rounded_rectangle(ctx,3+x,3+y,w-5,h-5,2.0)
ctx.set_source(pat)
ctx.fill()
else:
pat = cairo.LinearGradient(2+x,2+y,2+x,2+y+h-4)
pat.add_color_stop_rgba(0,1,1,1,1)
pat.add_color_stop_rgba(1,241/255,241/255,244/255,1)
rounded_rectangle(ctx,2+x,2+y,w-4,h-4,2.0)
ctx.set_source(pat)
ctx.fill()
pat = cairo.LinearGradient(3+x,3+y,3+x,3+y+h-4)
pat.add_color_stop_rgba(0,252/255,252/255,254/255,1)
pat.add_color_stop_rgba(1,223/255,225/255,230/255,1)
rounded_rectangle(ctx,3+x,3+y,w-5,h-5,2.0)
ctx.set_source(pat)
ctx.fill()
ctx.restore()
@ -103,9 +113,11 @@ class Button(object):
def __init__(self, text, callback):
self.text = text
self.callback = callback
self.hilight = False
self.hilight = 0
self.x, self.y, self.width, self.height = 0,0,0,0
def draw(self, window, ctx, x, y, w, h):
self.x, self.y, self.width, self.height = x, y, w, h
draw_button(ctx,x,y,w,h,self.hilight)
x_, y_ = ctx.user_to_device(x,y)
@ -116,10 +128,10 @@ class Button(object):
tr.draw(window)
def focus_enter(self):
self.hilight = True
self.hilight = 1
def focus_leave(self):
self.hilight = False
self.hilight = 0
class CalculatorWindow(yutani.Window):
@ -159,6 +171,7 @@ class CalculatorWindow(yutani.Window):
self.error = False
self.hover_widget = None
self.down_button = None
def calculate(self):
if self.error or len(self.tr.text) == 0:
@ -249,27 +262,46 @@ class CalculatorWindow(yutani.Window):
w,h = self.width - self.decorator.width(), self.height - self.decorator.height()
redraw = False
if y > self.tr.height and y < h and x >= 0 and x < w:
row = int((y - self.tr.height) / (self.height - self.decorator.height() - self.tr.height) * len(self.buttons))
col = int(x / (self.width - self.decorator.width()) * len(self.buttons[row]))
button = self.buttons[row][col]
if button != self.hover_widget:
if button:
button.focus_enter()
redraw = True
if self.down_button:
if msg.command == yutani.MouseEvent.RAISE or msg.command == yutani.MouseEvent.CLICK:
if not (msg.buttons & yutani.MouseButton.BUTTON_LEFT):
if x >= self.down_button.x and \
x < self.down_button.x + self.down_button.width and \
y >= self.down_button.y and \
y < self.down_button.y + self.down_button.height:
self.down_button.focus_enter()
self.down_button.callback(self.down_button)
self.down_button = None
redraw = True
else:
self.down_button.focus_leave()
self.down_button = None
redraw = True
else:
if y > self.tr.height and y < h and x >= 0 and x < w:
row = int((y - self.tr.height) / (self.height - self.decorator.height() - self.tr.height) * len(self.buttons))
col = int(x / (self.width - self.decorator.width()) * len(self.buttons[row]))
button = self.buttons[row][col]
if button != self.hover_widget:
if button:
button.focus_enter()
redraw = True
if self.hover_widget:
self.hover_widget.focus_leave()
redraw = True
self.hover_widget = button
if msg.command == yutani.MouseEvent.DOWN:
if button:
button.hilight = 2
self.down_button = button
redraw = True
else:
if self.hover_widget:
self.hover_widget.focus_leave()
redraw = True
self.hover_widget = button
if msg.command == 0:
if button:
button.callback(button)
else:
if self.hover_widget:
self.hover_widget.focus_leave()
redraw = True
self.hover_widget = None
self.hover_widget = None
if redraw:
self.draw()