z_mirror/TaskList

git clone https://git.raiguard.me/z_mirror/TaskList

Caleb HeuerGet the mod working againd03b0d7

trunk
4.3 KiB152 linesraw
1local constants = require("constants")
2
3local templates = {}
4
5--- @param name string
6--- @param visible boolean|nil
7function templates.checkboxes_flow(name, visible)
8  return {
9    type = "flow",
10    direction = "vertical",
11    ref = { name .. "_flow" },
12    { type = "flow", name = "incompleted", direction = "vertical", visible = false },
13    { type = "flow", name = "completed", direction = "vertical", visible = false },
14    visible = visible,
15  }
16end
17
18--- @param sprite string
19--- @param tooltip LocalisedString|nil
20--- @param action table|nil
21--- @param ref table|nil
22function templates.frame_action_button(sprite, tooltip, action, ref)
23  return {
24    type = "sprite-button",
25    style = "frame_action_button",
26    sprite = sprite,
27    tooltip = tooltip,
28    ref = ref,
29    actions = {
30      on_click = action,
31    },
32  }
33end
34
35-- XXX: See https://forums.factorio.com/100896
36local toplevel_width = 452
37local nesting_offset = 22
38--- @param Task Task
39--- @return number
40local function get_task_description_width(Task)
41  local nesting_level = 0
42  repeat
43    Task = Task.owner --[[@as Task?]]
44    nesting_level = nesting_level + 1
45  until Task.object_name ~= "Task"
46
47  return toplevel_width - (nesting_offset * nesting_level)
48end
49
50--- @param Task Task
51--- @param index number|nil
52--- @param completed boolean
53function templates.task_item(Task, index, completed)
54  return {
55    type = "flow",
56    name = Task.id,
57    direction = "vertical",
58    index = index or nil,
59    {
60      type = "flow",
61      style = "tlst_task_item_flow",
62      {
63        type = "checkbox",
64        style_mods = { horizontally_stretchable = true, horizontally_squashable = true },
65        caption = Task.title,
66        state = completed,
67        actions = {
68          on_checked_state_changed = { gui = "tasks", action = "toggle_task_completed", task_id = Task.id },
69        },
70      },
71      {
72        type = "label",
73        style = "info_label",
74        caption = Task.assignee and Task.assignee.name or nil,
75        visible = Task.assignee and true or false,
76      },
77      {
78        type = "sprite",
79        style = "flib_indicator",
80        sprite = "flib_indicator_" .. constants.task_status[Task.status].color,
81        tooltip = constants.task_status[Task.status].label,
82        actions = {
83          on_click = { gui = "tasks", action = "cycle_task_status", task_id = Task.id },
84        },
85      },
86      {
87        type = "sprite-button",
88        style = "mini_button_aligned_to_text_vertically_when_centered",
89        sprite = "utility/rename_icon",
90        tooltip = { "gui.tlst-edit-task" },
91        actions = {
92          on_click = {
93            gui = "tasks",
94            action = "edit_task",
95            task_id = Task.id,
96            parent_task_id = Task.owner.object_name == "Task" and Task.owner.id or nil,
97          },
98        },
99      },
100      {
101        type = "sprite-button",
102        style = "mini_button_aligned_to_text_vertically_when_centered",
103        sprite = "tlst_arrow_right",
104        tooltip = { "gui.tlst-expand-tooltip" },
105        actions = {
106          on_click = { gui = "tasks", transform = "handle_expand_click", task_id = Task.id },
107        },
108      },
109    },
110    {
111      type = "flow",
112      name = "details_flow",
113      style_mods = { left_margin = 22 },
114      direction = "vertical",
115      visible = false,
116      {
117        type = "label",
118        style_mods = {
119          font_color = constants.description_color,
120          single_line = false,
121          width = get_task_description_width(Task),
122        },
123        caption = Task.description,
124        visible = #Task.description > 0,
125      },
126      {
127        type = "flow",
128        name = "subtasks_flow",
129        direction = "vertical",
130        { type = "flow", name = "incompleted", direction = "vertical", visible = false },
131        { type = "flow", name = "completed", direction = "vertical", visible = false },
132        {
133          type = "flow",
134          style = "tlst_task_item_flow",
135          {
136            type = "label",
137            style = "tlst_new_task_label",
138            caption = { "gui.tlst-add-subtask" },
139            actions = {
140              on_click = { gui = "tasks", action = "edit_task", parent_task_id = Task.id },
141            },
142          },
143          actions = {
144            on_click = { gui = "tasks", action = "edit_task", parent_task_id = Task.id },
145          },
146        },
147      },
148    },
149  }
150end
151
152return templates