aboutsummaryrefslogtreecommitdiff
path: root/src/state.c
blob: 2051652cb3ca3525475591af7d3aa3b32c56fec6 (plain)
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
#include <glib.h>

#include "state.h"

State *state_create(Window *win, Drawer *draw)
{
    State *state = g_malloc(sizeof(State));
    g_assert_nonnull(state);

    state->win = win;
    state->draw = draw;
    state->btns = NULL;
    state->id = 0;

    return state;
}

void state_add_button(State *state, Button *btn)
{
    state->btns = g_list_append(state->btns, btn);
}

void state_remove_button(State *state, Button *btn)
{
    state->btns = g_list_remove(state->btns, btn);
}

static gboolean redraw_handler(gpointer data)
{
    State *state = data;

    if (state->relayout) {
        draw_compute_layout(state->draw, state->win, state->btns);
        state->relayout = false;
    }

    draw_paint(state->draw, state->win);

    state->id = 0;
    return G_SOURCE_REMOVE;
}

void state_redraw(State *state, bool changed_layout)
{
    if (state->id != 0) {
        if (!changed_layout || state->relayout)
            return;

        // If we should relayout override old redraw
        g_source_remove(state->id);
    }

    state->relayout = changed_layout;
    state->id = g_idle_add_full(G_PRIORITY_HIGH_IDLE, redraw_handler, state, NULL);
}

static gint align_compare(gconstpointer a, gconstpointer b)
{
    PangoAlignment a_align = ((Button *)a)->align;
    PangoAlignment b_align = ((Button *)b)->align;

    if (a_align < b_align) return -1;
    if (a_align > b_align) return 1;
    return 0;
}

void state_order_button(State *state)
{
    state->btns = g_list_sort(state->btns, align_compare);
}

void state_destroy(State *state)
{
    g_list_free_full(state->btns, (void *)button_destroy);
    g_free(state);
}

// vim: ts=4 sw=4 et