Class: Crimson::Window

Inherits:
Object show all
Defined in:
lib/crimson/widgets/window.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#added_children, #event_handlers, #id, #node, #removed_children, #tag

Attributes inherited from Model

#local, #observers, #revision_number, #revisions

Instance Method Summary collapse

Methods inherited from Object

#==, #add, #breadth_each, #children, #commit_tree!, #eql?, #find_descendant, #hash, #hidden?, #hide, #inspect, #move, #on, #on_event, #parent, #parent=, #postordered_each, #preordered_each, #remove, #root, #show, #shown?, #siblings, #to_s, #un

Methods inherited from Model

#add_observer, #apply_changes!, #changed?, #changes, #commit!, #master, #modify, #new_changes, #notify_observers, #reload!, #remove_observer, #rollback!

Constructor Details

#initialize(title, width = '800px', height = '600px') ⇒ Window

Returns a new instance of Window.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crimson/widgets/window.rb', line 16

def initialize(title, width = '800px', height = '600px')
  super(:div)

  @resizable = true
  @offset = Hashie::Mash.new
  @previous_dimensions = Hashie::Mash.new

  @titlebar = Titlebar.new(title)
  titlebar.draggable = true
  titlebar.on('dragstart', method(:on_dragstart))
  titlebar.resize_button.on('click') do |_data|
    maximized? ? minimize : maximize
    commit_tree!
  end
  titlebar.close_button.on('click') do |_data|
    old_parent = parent
    old_parent.remove(self)
    old_parent.commit_tree!
  end
  add(titlebar)

  @left_resizer = LeftResizer.new
  add(left_resizer)

  @right_resizer = RightResizer.new
  add(right_resizer)

  @top_resizer = TopResizer.new
  top_resizer.style.height = '10px'
  add(top_resizer)

  @bottom_resizer = BottomResizer.new
  add(bottom_resizer)

  @content = Crimson::Object.new(:div)
  content.style.width = '100%'
  content.style.height = "calc(100% - #{titlebar.style.height})"
  content.style.padding = 0
  content.style.margin = 0
  add(content)

  style.left = '0px'
  style.top = '0px'
  style.height = height
  style.width = width
  style.minHeight = '200px'
  style.minWidth = '400px'
  style.position = 'absolute'
  style.backgroundColor = 'white'
  style.boxShadow = '0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)'

  on('mousedown', method(:on_mousedown))
end

Instance Attribute Details

#bottom_resizerObject (readonly)

Returns the value of attribute bottom_resizer.



14
15
16
# File 'lib/crimson/widgets/window.rb', line 14

def bottom_resizer
  @bottom_resizer
end

#contentObject

Returns the value of attribute content.



13
14
15
# File 'lib/crimson/widgets/window.rb', line 13

def content
  @content
end

#left_resizerObject (readonly)

Returns the value of attribute left_resizer.



14
15
16
# File 'lib/crimson/widgets/window.rb', line 14

def left_resizer
  @left_resizer
end

#offsetObject (readonly)

Returns the value of attribute offset.



13
14
15
# File 'lib/crimson/widgets/window.rb', line 13

def offset
  @offset
end

#previous_dimensionsObject (readonly)

Returns the value of attribute previous_dimensions.



13
14
15
# File 'lib/crimson/widgets/window.rb', line 13

def previous_dimensions
  @previous_dimensions
end

#resizableObject

Returns the value of attribute resizable.



14
15
16
# File 'lib/crimson/widgets/window.rb', line 14

def resizable
  @resizable
end

#right_resizerObject (readonly)

Returns the value of attribute right_resizer.



14
15
16
# File 'lib/crimson/widgets/window.rb', line 14

def right_resizer
  @right_resizer
end

#titlebarObject (readonly)

Returns the value of attribute titlebar.



13
14
15
# File 'lib/crimson/widgets/window.rb', line 13

def titlebar
  @titlebar
end

#top_resizerObject (readonly)

Returns the value of attribute top_resizer.



14
15
16
# File 'lib/crimson/widgets/window.rb', line 14

def top_resizer
  @top_resizer
end

Instance Method Details

#maximizeObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/crimson/widgets/window.rb', line 111

def maximize
  self.resizable = false

  previous_dimensions.top = style.top.dup
  previous_dimensions.left = style.left.dup
  previous_dimensions.width = style.width.dup
  previous_dimensions.height = style.height.dup

  style.top = '0px'
  style.left = '0px'
  style.width = '100vw'
  style.height = '100vh'
end

#maximized?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/crimson/widgets/window.rb', line 85

def maximized?
  style.height == '100vh' && style.width == '100vw'
end

#minimizeObject



125
126
127
128
129
130
131
132
# File 'lib/crimson/widgets/window.rb', line 125

def minimize
  style.top = previous_dimensions.top.dup
  style.left =  previous_dimensions.left.dup
  style.width = previous_dimensions.width.dup
  style.height = previous_dimensions.height.dup

  self.resizable = true
end

#minimized?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/crimson/widgets/window.rb', line 89

def minimized?
  !maximized?
end

#on_dragstart(data) ⇒ Object



75
76
77
78
# File 'lib/crimson/widgets/window.rb', line 75

def on_dragstart(data)
  offset.top = style.top.delete_suffix('px').to_i - data.clientY
  offset.left = style.left.delete_suffix('px').to_i - data.clientX
end

#on_mousedown(_data) ⇒ Object



80
81
82
83
# File 'lib/crimson/widgets/window.rb', line 80

def on_mousedown(_data)
  parent.move(self, -1)
  parent.commit_tree!(:children)
end

#resizable?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/crimson/widgets/window.rb', line 93

def resizable?
  resizable
end

#resizersObject



107
108
109
# File 'lib/crimson/widgets/window.rb', line 107

def resizers
  [left_resizer, right_resizer, top_resizer, bottom_resizer]
end