File size: 1,301 Bytes
402daee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
--[[

  AlertPopup.lua - Alert popup UI

]]--

AlertPopup = Polo {
  WIDTH = 400,
  HEIGHT = 200,
  MIN_CONTENT_WIDTH = 375,
  BUTTON_WIDTH = 120,
  DEFAULT_TITLE = 'Alert',
}

function AlertPopup:init()
  self.title = self.title or self.DEFAULT_TITLE
  self.msg = ''
end

function AlertPopup:show(msg)
  self.msg = msg
  self:open()
end

function AlertPopup:render()
  local center = {ImGui.Viewport_GetCenter(ImGui.GetWindowViewport(ctx))}
  ImGui.SetNextWindowPos(ctx, center[1], center[2], ImGui.Cond_Appearing(), 0.5, 0.5)
  ImGui.SetNextWindowSize(ctx, self.WIDTH, self.HEIGHT, ImGui.Cond_FirstUseEver())

  if ImGui.BeginPopupModal(ctx, self.title, true, ImGui.WindowFlags_AlwaysAutoResize()) then
    app:trap(function () self:render_content() end)
    ImGui.EndPopup(ctx)
  else
    self:close()
  end
end

function AlertPopup:render_content()
  ImGui.Text(ctx, self.msg)
  self:render_separator()
  if ImGui.Button(ctx, 'OK', self.BUTTON_WIDTH, 0) then
    self:close()
  end
end

function AlertPopup:render_separator()
  ImGui.Dummy(ctx, self.MIN_CONTENT_WIDTH, 0)
  ImGui.Separator(ctx)
  ImGui.Dummy(ctx, 0, 0)
end

function AlertPopup:open()
  ImGui.OpenPopup(ctx, self.title)
end

function AlertPopup:close()
  ImGui.CloseCurrentPopup(ctx)
  if self.onclose then self.onclose() end
end