summaryrefslogtreecommitdiffstats
path: root/src/shell-workspace-background.c
blob: 1c0bbc380087cbd302d1c3aaee10d0963196c456 (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "config.h"

#include "shell-workspace-background.h"

#include "shell-global.h"
#include <meta/meta-workspace-manager.h>

#define BACKGROUND_MARGIN 12

enum
{
  PROP_0,

  PROP_MONITOR_INDEX,
  PROP_STATE_ADJUSTMENT_VALUE,

  PROP_LAST
};

static GParamSpec *obj_props[PROP_LAST] = { NULL, };

struct _ShellWorkspaceBackground
{
  /*< private >*/
  StWidget parent_instance;

  int monitor_index;
  double state_adjustment_value;

  MetaRectangle work_area;
  MetaRectangle monitor_geometry;
};

G_DEFINE_TYPE (ShellWorkspaceBackground, shell_workspace_background, ST_TYPE_WIDGET);

static void
on_workareas_changed (ShellWorkspaceBackground *self)
{
  ShellGlobal *global = shell_global_get ();
  MetaDisplay *display = shell_global_get_display (global);
  MetaWorkspaceManager *workspace_manager = shell_global_get_workspace_manager (global);
  MetaWorkspace *workspace =
    meta_workspace_manager_get_workspace_by_index (workspace_manager, 0);

  meta_workspace_get_work_area_for_monitor (workspace,
                                            self->monitor_index,
                                            &self->work_area);

  meta_display_get_monitor_geometry (display,
                                     self->monitor_index,
                                     &self->monitor_geometry);
}

static void
shell_workspace_background_allocate (ClutterActor          *actor,
                                     const ClutterActorBox *box)
{
  ShellWorkspaceBackground *self = SHELL_WORKSPACE_BACKGROUND (actor);
  ShellGlobal *global = shell_global_get ();
  ClutterStage *stage = shell_global_get_stage (global);
  StThemeContext *context = st_theme_context_get_for_stage (stage);
  StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (actor));
  ClutterActorBox scaled_box, my_box, content_box;
  ClutterActor *child;
  float content_width, content_height;
  float scaled_width, scaled_height;
  float x_scale, y_scale;
  float width, height;
  int left_offset, right_offset;
  int top_offset, bottom_offset;
  int scale_factor;

  scale_factor = st_theme_context_get_scale_factor (context);

  clutter_actor_box_get_size (box, &width, &height);
  scaled_height = height - BACKGROUND_MARGIN * 2 * scale_factor;
  scaled_width = (scaled_height / height) * width;

  scaled_box.x1 = box->x1 + (width - scaled_width) / 2;
  scaled_box.y1 = box->y1 + (height - scaled_height) / 2;
  clutter_actor_box_set_size (&scaled_box, scaled_width, scaled_height);

  clutter_actor_box_interpolate(box, &scaled_box,
                                self->state_adjustment_value, &my_box);

  clutter_actor_set_allocation (actor, &my_box);

  st_theme_node_get_content_box (theme_node, &my_box, &content_box);

  child = clutter_actor_get_first_child (actor);
  clutter_actor_allocate (child, &content_box);

  clutter_actor_box_get_size (&content_box, &content_width, &content_height);
  x_scale = content_width / self->work_area.width;
  y_scale = content_height / self->work_area.height;

  left_offset = self->work_area.x - self->monitor_geometry.x;
  top_offset = self->work_area.y - self->monitor_geometry.y;
  right_offset = self->monitor_geometry.width - self->work_area.width - left_offset;
  bottom_offset = self->monitor_geometry.height - self->work_area.height - top_offset;

  clutter_actor_box_set_origin (&content_box,
                                -left_offset * x_scale,
                                -top_offset * y_scale);
  clutter_actor_box_set_size (&content_box,
                              content_width + (left_offset + right_offset) * x_scale,
                              content_height + (top_offset + bottom_offset) * y_scale);

  child = clutter_actor_get_first_child (child);
  clutter_actor_allocate (child, &content_box);
}

static void
shell_workspace_background_constructed (GObject *object)
{
  G_OBJECT_CLASS (shell_workspace_background_parent_class)->constructed (object);

  on_workareas_changed (SHELL_WORKSPACE_BACKGROUND (object));
}

static void
shell_workspace_background_get_property (GObject      *gobject,
                                         unsigned int  property_id,
                                         GValue       *value,
                                         GParamSpec   *pspec)
{
  ShellWorkspaceBackground *self = SHELL_WORKSPACE_BACKGROUND (gobject);

  switch (property_id)
    {
    case PROP_MONITOR_INDEX:
      g_value_set_int (value, self->monitor_index);
      break;

    case PROP_STATE_ADJUSTMENT_VALUE:
      g_value_set_double (value, self->state_adjustment_value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, property_id, pspec);
    }
}

static void
shell_workspace_background_set_property (GObject      *gobject,
                                         unsigned int  property_id,
                                         const GValue *value,
                                         GParamSpec   *pspec)
{
  ShellWorkspaceBackground *self = SHELL_WORKSPACE_BACKGROUND (gobject);

  switch (property_id)
    {
    case PROP_MONITOR_INDEX:
      {
        int new_value = g_value_get_int (value);
        if (self->monitor_index != new_value)
        {
          self->monitor_index = new_value;
          g_object_notify_by_pspec (gobject, obj_props[PROP_MONITOR_INDEX]);
        }
      }
      break;

    case PROP_STATE_ADJUSTMENT_VALUE:
      {
        double new_value = g_value_get_double (value);
        if (self->state_adjustment_value != new_value)
        {
          self->state_adjustment_value = new_value;
          g_object_notify_by_pspec (gobject, obj_props[PROP_STATE_ADJUSTMENT_VALUE]);
        }
      }
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, property_id, pspec);
    }
}

static void
shell_workspace_background_class_init (ShellWorkspaceBackgroundClass *klass)
{
  ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  actor_class->allocate = shell_workspace_background_allocate;

  gobject_class->constructed = shell_workspace_background_constructed;
  gobject_class->get_property = shell_workspace_background_get_property;
  gobject_class->set_property = shell_workspace_background_set_property;

  /**
   * ShellWorkspaceBackground:monitor-index:
   */
  obj_props[PROP_MONITOR_INDEX] =
    g_param_spec_int ("monitor-index", "", "",
                      0, G_MAXINT, 0,
                      G_PARAM_READWRITE |
                      G_PARAM_CONSTRUCT_ONLY |
                      G_PARAM_STATIC_STRINGS |
                      G_PARAM_EXPLICIT_NOTIFY);

  /**
   * ShellWorkspaceBackground:state-adjustment-value:
   */
  obj_props[PROP_STATE_ADJUSTMENT_VALUE] =
    g_param_spec_double ("state-adjustment-value", "", "",
                         -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS |
                         G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
}

static void
shell_workspace_background_init (ShellWorkspaceBackground *self)
{
  ShellGlobal *global = shell_global_get ();
  MetaDisplay *display = shell_global_get_display (global);

  g_signal_connect_object (display, "workareas-changed",
                           G_CALLBACK (on_workareas_changed),
                           self, G_CONNECT_SWAPPED);
}