~ruther/CTU-FEE-B0B35APO-Semestral-project

CTU-FEE-B0B35APO-Semestral-project/file-browser/src/gui_list_commands.c -rw-r--r-- 5.3 KiB
7aa0ada9 — Rutherther chore: fix image in readme 2 years ago
                                                                                
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "gui_list_commands.h"
#include "direction.h"
#include "gui.h"
#include "input.h"
#include "keyboard_const.h"
#include "rotation_const.h"

static void command_handler_gui_list_clicked(void *state, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t*)state;
  if (amount > 1) {
    click_state->clicked(click_state->container, click_state->state,
                         gui_list_get_selected_index(click_state->container));
  }
}

static void command_handler_zoom_in(void *state,
                                 int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t *)state;
  if (click_state->window == click_state->gui->active_window &&
      click_state->font != NULL && amount != 0) {
    click_state->font->size += amount > 0 ? 1 : -1;
    gui_list_container_set_item_height(click_state->container,
                                       click_state->font->size);
  }
}

static void command_handler_zoom_out(void *state,
                                    int amount) {
  command_handler_zoom_in(state, -amount);
}

static void command_handler_move(void *state, direction_t direction, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t *)state;
  if (click_state->window == click_state->gui->active_window) {
    int32_t x = 0, y = 0;
    direction_move_xy(direction, &x, &y, amount);
    gui_list_scroll(click_state->container, (int16_t)x, (int16_t)y);
  }
}

static void command_handler_move_left(void *state, int amount) {
  command_handler_move(state, LEFT, amount);
}

static void command_handler_move_right(void *state, int amount) {
  command_handler_move(state, RIGHT, amount);
}

static void command_handler_move_down(void *state, int amount) {
  command_handler_move(state, DOWN, amount);
}

static void command_handler_move_up(void *state, int amount) {
  command_handler_move(state, UP, amount);
}

static void command_handler_home(void *state, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t *)state;
  if (click_state->window == click_state->gui->active_window) {
    gui_list_set_selected_index(click_state->container, 0);
  }
}

static void command_handler_end(void *state, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t *)state;
  if (click_state->window == click_state->gui->active_window) {
    gui_list_set_selected_index(click_state->container, gui_list_get_items_count(click_state->container));
  }
}

static void command_handler_page_down(void *state, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t *)state;
  if (click_state->window == click_state->gui->active_window) {
    uint32_t selected_index = gui_list_get_selected_index(click_state->container);
    gui_list_set_selected_index(
        click_state->container,
        selected_index +
            gui_list_get_visible_items_count(click_state->container));
  }
}

static void command_handler_page_up(void *state, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t *)state;
  if (click_state->window == click_state->gui->active_window) {
    uint32_t selected_index =
        gui_list_get_selected_index(click_state->container);
    uint32_t visible_items = gui_list_get_visible_items_count(click_state->container);

    if (selected_index < visible_items) {
      selected_index = 0;
    } else {
      selected_index -= visible_items;
    }

    gui_list_set_selected_index(
        click_state->container,
        selected_index);
  }
}

void gui_list_commands_register(commands_t *commands, gui_list_command_state_t *state) {
  commands_register(commands, IN_KEYBOARD, 13, command_handler_gui_list_clicked, state);
  commands_register(commands, IN_KEYBOARD, 'v', command_handler_gui_list_clicked,
                    state);

  commands_register(commands, IN_KEYBOARD, KEYBOARD_HOME, command_handler_home,
                    state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_END, command_handler_end,
                    state);

  commands_register(commands, IN_KEYBOARD, KEYBOARD_PAGE_DOWN,
                    command_handler_page_down, state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_PAGE_UP,
                    command_handler_page_up, state);

  commands_register(commands, IN_KEYBOARD, KEYBOARD_DOWN,
                    command_handler_move_down, state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_UP, command_handler_move_up,
                    state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_LEFT,
                    command_handler_move_left, state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_RIGHT,
                    command_handler_move_right, state);

  commands_register(commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_HORIZONTAL,
                    command_handler_move_right, state);
  commands_register(commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_VERTICAL,
                    command_handler_move_down, state);

  if (state->font != NULL) {
    commands_register(commands, IN_KEYBOARD, KEYBOARD_ZOOM_IN,
                      command_handler_zoom_in, state);
    commands_register(commands, IN_KEYBOARD, KEYBOARD_ZOOM_OUT,
                      command_handler_zoom_out, state);
    commands_register(commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_ZOOM,
                      command_handler_zoom_in, state);
  }
}