Class: MotionKit::NSWindowHelpers

Inherits:
WindowLayout show all
Defined in:
lib/motion-kit-osx/helpers/nswindow_helpers.rb,
lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb

Direct Known Subclasses

NSWindowLayout

Instance Attribute Summary

Attributes inherited from BaseLayout

#parent

Instance Method Summary collapse

Methods inherited from WindowLayout

#add_child, #default_root, #remove_child, #window

Methods inherited from TreeLayout

#add, #all, #all_views, #build, #built?, #child_layouts, #create, #create_default_root_context, #first, #forget, #get, #get_view, #initial, #initial?, #initialize, #last, #last_view, #name_element, #nearest, #next, #nth, #nth_view, #prev, #reapply, #reapply!, #reapply?, #remove, #root, view, #view

Methods inherited from BaseLayout

#add_deferred_block, #apply, #apply_with_context, #apply_with_target, #context, #deferred, #deferred_blocks, delegate_method_fix, #has_context?, #initialize, #ipad?, #iphone35?, #iphone4?, #iphone?, #is_parent_layout?, #method_missing, #objc_version, #orientation?, #parent_layout, #retina?, #ruby_version, #run_deferred, #set_parent_layout, #target, #v

Methods included from BaseLayoutClassMethods

#layout_for, #memoize, #target_klasses, #targets

Constructor Details

This class inherits a constructor from MotionKit::TreeLayout

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MotionKit::BaseLayout

Instance Method Details

#_calculate_frame(f, from: from_window, relative_to: point) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 146

def _calculate_frame(f, from: from_window, relative_to: point)
  if from_window.is_a?(Symbol)
    from_window = self.get_view(from_window)
  end

  from_window_size = from_window.frame.size

  calculate_window = target

  if point[:x] == :reset || point[:y] == :reset
    calculate_window = NSWindow.alloc.init
    calculate_window.setFrame([[0, 0], target.frame.size], display: false)
  end

  if f.is_a?(Hash)
    f = f.merge(relative: true, flipped: true)
  end
  f = MotionKit.calculate(calculate_window, :frame, f, from_window)
  if from_window.is_a?(NSWindow)
    f.origin.x += from_window.frame.origin.x
    f.origin.y += from_window.frame.origin.y
  end

  case point[:x]
  when :min, :reset
    # pass
  when :mid
    f.origin.x += (from_window_size.width - f.size.width) / 2.0
  when :max
    f.origin.x += from_window_size.width - f.size.width
  when :before
    f.origin.x -= f.size.width
  when :after
    f.origin.x += from_window_size.width
  else
    f.origin.x += point[:x]
  end

  case point[:y]
  when :reset, :min
    # pass
  when :mid
    f.origin.y += (from_window_size.height - f.size.height) / 2.0
  when :max
    f.origin.y += from_window_size.height - f.size.height
  when :above
    f.origin.y += from_window_size.height
  when :below
    f.origin.y -= f.size.height
  else
    f.origin.y += point[:y]
  end

  return f
end

#_fix_frame_value(value) ⇒ Object



5
6
7
8
9
10
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 5

def _fix_frame_value(value)
  if value.is_a?(Hash) && value[:relative]
    return value.merge(flipped: true)
  end
  return value
end

#above(from_window, f = {}) ⇒ Object

The first arg can be a window or a frame



330
331
332
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 330

def above(from_window, f={})
  _calculate_frame(f, from: from_window, relative_to: { x: :reset, y: :above })
end

#after(from_window, f = {}) ⇒ Object Also known as: right_of

The first arg can be a window or a frame



349
350
351
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 349

def after(from_window, f={})
  _calculate_frame(f, from: from_window, relative_to: { x: :after, y: :reset })
end

#before(from_window, f = {}) ⇒ Object Also known as: left_of

The first arg can be a window or a frame



342
343
344
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 342

def before(from_window, f={})
  _calculate_frame(f, from: from_window, relative_to: { x: :before, y: :reset })
end

#below(from_window, f = {}) ⇒ Object

The first arg can be a window or a frame



336
337
338
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 336

def below(from_window, f={})
  _calculate_frame(f, from: from_window, relative_to: { x: :reset, y: :below })
end

#bottom(value) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 62

def bottom(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin.y = MotionKit.calculate(target, :height, value, screen)
  target.setFrame(f, display: true)

  return CGRectGetMinY(target.frame)
end

#center(value) ⇒ Object Also known as: middle



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 123

def center(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  center = MotionKit.calculate(target, :center, value, screen)
  origin = CGPoint.new(center.x, center.y)
  origin.x -= f.size.width / 2
  origin.y -= f.size.height / 2
  f.origin = origin
  target.setFrame(f, display: true)
  return center
end

#center_x(value) ⇒ Object Also known as: middle_x



42
43
44
45
46
47
48
49
50
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 42

def center_x(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin.x = MotionKit.calculate(target, :width, value, screen)
  f.origin.x -= f.size.width / 2
  target.setFrame(f, display: true)
  return CGRectGetMidX(target.frame)
end

#center_y(value) ⇒ Object Also known as: middle_y



83
84
85
86
87
88
89
90
91
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 83

def center_y(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin.y = MotionKit.calculate(target, :height, value, screen)
  f.origin.y -= f.size.height / 2
  target.setFrame(f, display: true)
  return CGRectGetMidY(target.frame)
end

#frame(value, autosave_name = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 12

def frame(value, autosave_name=nil)
  value = _fix_frame_value(value)
  screen = target.screen || NSScreen.mainScreen
  value = MotionKit.calculate(target, :frame, value, screen)
  target.setFrame(value, display: true)
  if autosave_name
    target.setFrameAutosaveName(autosave_name)
  end
  return target.frame
end

#from_bottom(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_bottom(width: 80, height: 22)
frame from_bottom(another_view, width: 80, height: 22)


304
305
306
307
308
309
310
311
312
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 304

def from_bottom(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :mid, y: :min })
end

#from_bottom_left(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_bottom_left(width: 80, height: 22)
frame from_bottom_left(another_view, width: 80, height: 22)


290
291
292
293
294
295
296
297
298
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 290

def from_bottom_left(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :min, y: :min })
end

#from_bottom_right(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_bottom_right(width: 80, height: 22)
frame from_bottom_right(another_view, width: 80, height: 22)


318
319
320
321
322
323
324
325
326
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 318

def from_bottom_right(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :max, y: :min })
end

#from_center(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_center(width: 80, height: 22)
frame from_center(another_view, width: 80, height: 22)


262
263
264
265
266
267
268
269
270
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 262

def from_center(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :mid, y: :mid })
end

#from_left(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_left(width: 80, height: 22)
frame from_left(another_view, width: 80, height: 22)


248
249
250
251
252
253
254
255
256
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 248

def from_left(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :min, y: :mid })
end

#from_right(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_right(width: 80, height: 22)
frame from_right(another_view, width: 80, height: 22)


276
277
278
279
280
281
282
283
284
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 276

def from_right(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :max, y: :mid })
end

#from_top(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_top(width: 80, height: 22)
frame from_top(another_view, width: 80, height: 22)


220
221
222
223
224
225
226
227
228
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 220

def from_top(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :mid, y: :max })
end

#from_top_left(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_top_left(width: 80, height: 22)
frame from_top_left(another_view, width: 80, height: 22)


206
207
208
209
210
211
212
213
214
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 206

def from_top_left(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :min, y: :max })
end

#from_top_right(from_window = nil, f = nil) ⇒ Object

The first arg can be a window or a frame

Examples:

frame from_top_right(width: 80, height: 22)
frame from_top_right(another_view, width: 80, height: 22)


234
235
236
237
238
239
240
241
242
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 234

def from_top_right(from_window=nil, f=nil)
  if from_window.is_a?(Hash)
    f = from_window
    from_window = nil
  end
  f ||= {}
  from_window ||= target.screen || NSScreen.mainScreen
  _calculate_frame(f, from: from_window, relative_to: { x: :max, y: :max })
end

#height(value) ⇒ Object Also known as: h



104
105
106
107
108
109
110
111
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 104

def height(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.size.height = MotionKit.calculate(target, :height, value, screen)
  target.setFrame(f, display: true)
  return CGRectGetHeight(f)
end

#origin(value) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 114

def origin(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin = MotionKit.calculate(target, :origin, value, screen)
  target.setFrame(f, display: true)
  return target.frame.origin
end

#relative_to(from_window, f) ⇒ Object

The first arg must be a view



356
357
358
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 356

def relative_to(from_window, f)
  _calculate_frame(f, from: from_window, relative_to: { x: :reset, y: :reset })
end

#right(value) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 33

def right(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin.x = MotionKit.calculate(target, :width, value, screen) - f.size.width
  target.setFrame(f, display: true)
  return CGRectGetMaxX(f)
end

#size(value) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 137

def size(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.size = MotionKit.calculate(target, :size, value, screen)
  target.setFrame(f, display: true)
  return target.frame.size
end

#top(value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 72

def top(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin.y = MotionKit.calculate(target, :height, value, screen)
  f.origin.y -= f.size.height
  target.setFrame(f, display: true)

  return CGRectGetMaxY(f)
end

#width(value) ⇒ Object Also known as: w



94
95
96
97
98
99
100
101
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 94

def width(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.size.width = MotionKit.calculate(target, :width, value, screen)
  target.setFrame(f, display: true)
  return CGRectGetWidth(f)
end

#x(value) ⇒ Object Also known as: left



23
24
25
26
27
28
29
30
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 23

def x(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin.x = MotionKit.calculate(target, :width, value, screen)
  target.setFrame(f, display: true)
  return CGRectGetMinX(f)
end

#y(value) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/motion-kit-osx/helpers/nswindow_frame_helpers.rb', line 53

def y(value)
  value = _fix_frame_value(value)
  f = target.frame
  screen = target.screen || NSScreen.mainScreen
  f.origin.y = MotionKit.calculate(target, :height, value, screen)
  target.setFrame(f, display: true)
  return CGRectGetMinY(f)
end