Class: KnifeAdvisor::UI::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/knife_advisor/ui/window.rb

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ Window

Returns a new instance of Window.



23
24
25
26
27
28
29
30
31
# File 'lib/knife_advisor/ui/window.rb', line 23

def initialize(image)
  @image = image
  center!

  # Create a widget to show the image.
  TkLabel.new(root, background: 'systemTransparent', image: image) do
    place(x: 150, y: 200-(image.height/2))
  end
end

Instance Method Details

#append(msg) ⇒ Object



69
70
71
# File 'lib/knife_advisor/ui/window.rb', line 69

def append(msg)
  say("#{@msg} #{msg}")
end

#center!Object



44
45
46
47
48
49
50
51
# File 'lib/knife_advisor/ui/window.rb', line 44

def center!
  # Center the window.
  screen_width = TkWinfo.screenwidth(root)
  screen_height = TkWinfo.screenheight(root)
  x = (screen_width/2) - (@image.width/2) - 150
  y = (screen_height/2) - 200
  root.geometry("#{root.width}x#{root.height}+#{x}+#{y}")
end

#clear!Object



80
81
82
83
84
85
86
# File 'lib/knife_advisor/ui/window.rb', line 80

def clear!
  if @message_canvas
    @message_canvas.destroy
    @message_canvas = nil
    @text = nil
  end
end

#clear_buttons!Object



107
108
109
110
# File 'lib/knife_advisor/ui/window.rb', line 107

def clear_buttons!
  (@yes_button + @no_button).each(&:destroy)
  @yes_button = @no_button = nil
end

#draw_buttons!Object



88
89
90
91
92
93
# File 'lib/knife_advisor/ui/window.rb', line 88

def draw_buttons!
  @yes_button = rounded_rect(@message_canvas, 10, 170, 16, 85, 2, '#FFFFC7', 1)
  @yes_button << TkcText.new(@message_canvas, 48, 178, text: 'Yes')
  @no_button = rounded_rect(@message_canvas, 105, 170, 16, 85, 2, '#FFFFC7', 1)
  @no_button << TkcText.new(@message_canvas, 147, 178, text: 'No')
end

#on_no(&block) ⇒ Object



101
102
103
104
105
# File 'lib/knife_advisor/ui/window.rb', line 101

def on_no(&block)
  @no_button.each do |obj|
    obj.bind('1', &block)
  end
end

#on_yes(&block) ⇒ Object



95
96
97
98
99
# File 'lib/knife_advisor/ui/window.rb', line 95

def on_yes(&block)
  @yes_button.each do |obj|
    obj.bind('1', &block)
  end
end

#rootObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/knife_advisor/ui/window.rb', line 33

def root
  @root ||= TkRoot.new(background: 'systemTransparent', width: @image.width+150, height: (@image.height/2)+200).tap do |window|
    # Disable the WM decoration (title bar).
    Tk::Wm.overrideredirect(window, 1)
    # Start in front of other windows.
    Tk::Wm.attributes(window, 'topmost', true)
    # Activate transparency mode.
    Tk::Wm.attributes(window, 'transparent', true)
  end
end

#rounded_rect(canvas, x, y, height, width, radius, color, border = 0, border_color = 'black') ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/knife_advisor/ui/window.rb', line 112

def rounded_rect(canvas, x, y, height, width, radius, color, border=0, border_color='black')
  [].tap do |objs|
    diameter = radius * 2
    # Top left corner.
    objs << TkcOval.new(canvas, x, y, x+diameter, y+diameter, fill: color, width: border, outline: border_color)
    # Top right corner.
    objs << TkcOval.new(canvas, x+width-diameter, y, x+width, y+diameter, fill: color, width: border, outline: border_color)
    # Bottom left corner.
    objs << TkcOval.new(canvas, x, y+height-diameter, x+diameter, y+height, fill: color, width: border, outline: border_color)
    # Bottom right corner.
    objs << TkcOval.new(canvas, x+width-diameter, y+height-diameter, x+width, y+height, fill: color, width: border, outline: border_color)
    objs << TkcRectangle.new(canvas, x+radius, y, x+width-radius, y+height, fill: color, width: 0)
    objs << TkcRectangle.new(canvas, x, y+radius, x+width, y+height-radius, fill: color, width: 0)
    if border > 0
      # Top border.
      objs << TkcLine.new(canvas, x+radius, y, x+width-radius, y, fill: border_color, width: border)
      # Bottom border.
      objs << TkcLine.new(canvas, x+radius, y+height, x+width-radius, y+height, fill: border_color, width: border)
      # Left border.
      objs << TkcLine.new(canvas, x, y+radius, x, y+height-radius, fill: border_color, width: border)
      # Right border
      objs << TkcLine.new(canvas, x+width, y+radius, x+width, y+height-radius, fill: border_color, width: border)
    end
  end
end

#say(msg) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/knife_advisor/ui/window.rb', line 53

def say(msg)
  msg = msg.strip
  @msg = msg
  unless @message_canvas
    @message_canvas = TkCanvas.new(root, background: 'systemTransparent', highlightthickness: 0) do
      place(x: 0, y: 0, width: 200, height: 200)
    end
    rounded_rect(@message_canvas, 0, 0, 200, 200, 10, '#FFFFC7')
  end
  if @text
    @text.text = msg
  else
    @text = TkcText.new(@message_canvas, 10, 10, anchor: 'nw', width: 180, text: msg)
  end
end

#unsay!Object



73
74
75
76
77
78
# File 'lib/knife_advisor/ui/window.rb', line 73

def unsay!
  if @message_canvas && @text
    @text.destroy
    @text = nil
  end
end