aboutsummaryrefslogtreecommitdiff
path: root/src/state.c
blob: c5dc759e9ce42638a85c231398e48644e305a7e5 (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
79
#include <glib.h>

#include "state.h"

State *state_create(Window *win, Drawable *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)
{
    // insert sorted
    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_and_layout(gpointer data)
{
    State *state = data;
    draw_compute_layout(state->draw, state->win, state->btns);
    draw_paint(state->draw, state->win);
    state->id = 0;
    return G_SOURCE_REMOVE;
}

static gboolean redraw(gpointer data)
{
    State *state = data;
    draw_paint(state->draw, state->win);
    state->id = 0;
    return G_SOURCE_REMOVE;
}

// Sometimes we schedule multiple redraws in a row without actually checking if there are
// others already scheduled...
// This could probably be fixed by implementing a redraw source...
void state_redraw(State *state, bool changed_layout)
{
    // XXX: Override old redraw
    if (state->id != 0)
        g_source_remove(state->id);

    state->id = g_idle_add_full(G_PRIORITY_HIGH_IDLE, changed_layout ? redraw_and_layout : redraw, 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