File size: 2,650 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
DefineClass.XCascade = {
	__parents = { "XActionsView" },
	properties = {
		{ category = "General", id = "MenuEntries", editor = "text", default = "", },
		{ category = "General", id = "ShowIcons", editor = "bool", default = false, },
		{ category = "General", id = "IconReservedSpace", editor = "number", default = 0, },
		{ category = "General", id = "CollapseTitles", editor = "bool", default = false, },
		{ category = "General", id = "ItemTemplate", editor = "text", default = "XTextButton" }
	},
	IdNode = true,
	VAlign = "stretch",
	HandleMouse = true,
	idSubCascade = false,
}

function XCascade:Init()
	XSleekScroll:new({
		Id = "idScroll",
		Target = "idContainer",
		Dock = "right",
		Margins = box(1, 1, 1, 1),
		AutoHide = true,
		MinThumbSize = 30,
	}, self)
	XScrollArea:new({
		Id = "idContainer",
		VAlign = "top",
		LayoutMethod = "VList",
		VScroll = "idScroll",
	}, self)
end

function XCascade:OnDelete()
	if IsKindOf(self.parent, "XCascade") then
		self.parent:SetCollapsed(false)
	end
end

function XCascade:PopupAction(action_id, host, source)
	if self.idSubCascade then
		self.idSubCascade:Close()
	end
	local menu = g_Classes[self.class]:new({
		Id = "idSubCascade",
		MenuEntries = action_id,
		Dock = "right",
		GetActionsHost = function(self) return host end,
		IconReservedSpace = self.IconReservedSpace,
		ShowIcons = self.ShowIcons,
		CollapseTitles = self.CollapseTitles,
		ItemTemplate = self.ItemTemplate,
	}, self)
	menu:Open()
	menu:SetFocus()
	self:SetCollapsed(true)
end

function XCascade:SetCollapsed(collapsed)
	self.idContainer:SetMaxWidth(collapsed and self.CollapseTitles and self.IconReservedSpace or 1000000)
	if collapsed then
		self.idScroll:SetAutoHide(false)
		self.idScroll:SetVisible(false)
	else
		self.idScroll:SetAutoHide(true)
	end
end

function XCascade:OnMouseButtonDown(pt, button)
	if button == "L" then
		self:SetFocus()
		if self.idSubCascade then
			self.idSubCascade:Close()
		end
		return "break"
	end
	if button == "R" then
		self:Close()
		return "break"
	end
end

function XCascade:RebuildActions(host)
	local menu = self.MenuEntries
	local context = host.context
	self.idContainer:DeleteChildren()
	for _, action in ipairs(host:GetMenubarActions(menu)) do
		if host:FilterAction(action) then
			local entry = XTemplateSpawn(self.ItemTemplate, self.idContainer, action) 
			entry.action = action
			entry:SetProperty("Translate", action.ActionTranslate)
			entry:SetProperty("Text", action.ActionName)
			entry:SetProperty("IconReservedSpace", self.IconReservedSpace)
			if self.ShowIcons then
				entry:SetProperty("Icon", action.ActionIcon)
			end
			entry:Open()
		end
	end
end