summaryrefslogtreecommitdiff
path: root/src/bin/elementary/test_task_switcher.c
blob: 8e57a585ccc03c505e160f4a7d1922db76515762 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif
#include <Elementary.h>
#include <Eina.h>

#define APPS_COL 0
#define VIEWS_COL 1
#define PROPS_VIEW_COL 2

static Evas_Object *table = NULL;
static Elm_App_Client *app_selected = NULL;
static Eina_List *apps_list = NULL;

static void _app_view_clicked(void *data, Evas_Object *obj, void *event_info);
static void _apps_list_update(void);

static void _btn_close_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_App_Client_View *view = data;
   elm_app_client_view_close(view, NULL, NULL);
}

static void _btn_pause_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_App_Client_View *view = data;
   elm_app_client_view_pause(view, NULL, NULL);
}

static void _btn_resume_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_App_Client_View *view = data;
   elm_app_client_view_resume(view, NULL, NULL);
}

static void
_app_view_prop_changed_cb(void *data EINA_UNUSED, const Eo_Event *event)
{
   Elm_App_View_State state = ELM_APP_VIEW_STATE_UNKNOWN;

   state = elm_app_client_view_state_get(event->object);

   if (state == ELM_APP_VIEW_STATE_CLOSED)
     {
        Evas_Object *props = elm_table_child_get(table, PROPS_VIEW_COL, 0);
        elm_table_unpack(table, props);
        evas_object_del(props);
     }
   else
     _app_view_clicked(event->object, NULL, NULL);
}

static void
_app_view_clicked(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_App_Client_View *view = data;
   Evas_Object *view_props, *close_btn, *resume_btn, *pause_btn;
   const char *title = NULL, *icon = NULL;
   int new_events = 0, window = 0;
   unsigned short progress = 0;
   Elm_App_View_State state = ELM_APP_VIEW_STATE_UNKNOWN;
   char buffer[1024];

   view_props = elm_table_child_get(table, PROPS_VIEW_COL, 0);
   if (view_props)
     {
        Elm_App_Client_View *old_view;

        old_view = evas_object_data_del(view_props, "view");
        efl_event_callback_del(old_view, ELM_APP_CLIENT_VIEW_EVENT_PROPERTY_CHANGED, _app_view_prop_changed_cb, table);
        elm_list_clear(view_props);
     }
   else
     {
        view_props = elm_list_add(table);
        evas_object_size_hint_align_set(view_props, EVAS_HINT_FILL,
                                        EVAS_HINT_FILL);
        evas_object_show(view_props);
        elm_table_pack(table, view_props, PROPS_VIEW_COL, 0, 1, 1);
     }

   evas_object_data_set(view_props, "view", view);

   title = elm_app_client_view_title_get(view);
   icon = elm_app_client_view_icon_get(view);
   progress = elm_app_client_view_progress_get(view);
   new_events = elm_app_client_view_new_events_get(view);
   window = elm_app_client_view_window_get(view);
   efl_event_callback_add(view, ELM_APP_CLIENT_VIEW_EVENT_PROPERTY_CHANGED, _app_view_prop_changed_cb, table);

   snprintf(buffer, sizeof(buffer), "Title=%s", title);
   elm_list_item_append(view_props, buffer, NULL, NULL, NULL, NULL);

   snprintf(buffer, sizeof(buffer), "Icon=%s", icon);
   elm_list_item_append(view_props, buffer, NULL, NULL, NULL, NULL);

   snprintf(buffer, sizeof(buffer), "Progress=%d", progress);
   elm_list_item_append(view_props, buffer, NULL, NULL, NULL, NULL);

   snprintf(buffer, sizeof(buffer), "New events=%d", new_events);
   elm_list_item_append(view_props, buffer, NULL, NULL, NULL, NULL);

   snprintf(buffer, sizeof(buffer), "WindowID=%d", window);
   elm_list_item_append(view_props, buffer, NULL, NULL, NULL, NULL);

   state = elm_app_client_view_state_get(view);
   if (state == ELM_APP_VIEW_STATE_LIVE)
     snprintf(buffer, sizeof(buffer), "State=alive");
   else if (state == ELM_APP_VIEW_STATE_PAUSED)
     snprintf(buffer, sizeof(buffer), "State=paused");
   else if (state == ELM_APP_VIEW_STATE_CLOSED)
     snprintf(buffer, sizeof(buffer), "State=closed");
   else if (state == ELM_APP_VIEW_STATE_SHALLOW)
     snprintf(buffer, sizeof(buffer), "State=shallow");
   else
     snprintf(buffer, sizeof(buffer), "State=unknown");
   elm_list_item_append(view_props, buffer, NULL, NULL, NULL, NULL);

   close_btn = elm_button_add(view_props);
   elm_object_text_set(close_btn, "Close view");
   evas_object_smart_callback_add(close_btn, "clicked", _btn_close_cb, view);
   elm_list_item_append(view_props, NULL, close_btn, NULL, NULL, NULL);

   pause_btn = elm_button_add(view_props);
   elm_object_text_set(pause_btn, "Pause view");
   evas_object_smart_callback_add(pause_btn, "clicked", _btn_pause_cb, view);
   elm_list_item_append(view_props, NULL, pause_btn, NULL, NULL, NULL );

   resume_btn = elm_button_add(view_props);
   elm_object_text_set(resume_btn, "Resume view");
   evas_object_smart_callback_add(resume_btn, "clicked", _btn_resume_cb, view);
   elm_list_item_append(view_props, NULL, resume_btn, NULL, NULL, NULL );

   elm_list_go(view_props);
}

static void
_popup_close_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   evas_object_del(data);
}

static void app_client_view_open_cb(void *data EINA_UNUSED, Elm_App_Client_View *view, const char *error, const char *error_message)
{
   Evas_Object *popup, *btn_ok;
   char buf[1024];

   popup = elm_popup_add(table);

   if (view)
     {
        _app_view_clicked(view, NULL, NULL);
        return;
     }

   snprintf(buf, sizeof(buf), "Some error happen opening view: %s %s", error, error_message);
   elm_object_part_text_set(popup, "default", buf);

   btn_ok = elm_button_add(popup);
   elm_object_text_set(btn_ok, "Ok");
   elm_object_part_content_set(popup, "button1", btn_ok);
   evas_object_smart_callback_add(btn_ok, "clicked", _popup_close_cb, popup);

   elm_popup_orient_set(popup, ELM_POPUP_ORIENT_TOP);
   evas_object_show(popup);
}

static void _popup_btn_open_view_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *popup = data;
   Eina_Value *args = NULL;//TODO fill with args of popup
   Elm_App_Client *app = evas_object_data_get(popup, "app");

   elm_app_client_view_open(app, args, app_client_view_open_cb, NULL);

   evas_object_del(popup);
}

static void
_app_view_open(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_App_Client *app = data;
   Evas_Object *popup, *btn_open, *btn_cancel, *args_box;

   popup = elm_popup_add(table);
   elm_object_part_text_set(popup, "title,text", "Open a view");

   btn_open = elm_button_add(popup);
   elm_object_text_set(btn_open, "Open");
   elm_object_part_content_set(popup, "button1", btn_open);
   evas_object_smart_callback_add(btn_open, "clicked", _popup_btn_open_view_cb, popup);
   evas_object_show(btn_open);

   btn_cancel = elm_button_add(popup);
   elm_object_text_set(btn_cancel, "Cancel");
   elm_object_part_content_set(popup, "button2", btn_cancel);
   evas_object_smart_callback_add(btn_cancel, "clicked", _popup_close_cb, popup);
   evas_object_show(btn_cancel);

   args_box = elm_box_add(popup);
   elm_object_part_content_set(popup, "default", args_box);
   elm_object_part_text_set(popup, "default", "TODO: add some entrys to add some parameters to view.");

   evas_object_data_set(popup, "app", app);
   elm_popup_orient_set(popup, ELM_POPUP_ORIENT_TOP);
   evas_object_show(popup);
}

static void _app_close_all_views_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_App_Client *app = data;
   elm_app_client_view_all_close(app);
}

static void
_app_terminate_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_App_Client *app = data;
   elm_app_client_terminate(app);
}

static void
_app_clicked(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *views, *btn_close_all, *btn_terminate, *btn_open_view;
   Eina_Iterator *views_iter = NULL;
   Elm_App_Client_View *client_view;
   Elm_App_Client *app = data;

   app_selected = app;

   views = elm_table_child_get(table, VIEWS_COL, 0);
   if (!views)
     {
        views = elm_list_add(table);
        evas_object_size_hint_align_set(views, EVAS_HINT_FILL, EVAS_HINT_FILL);
        evas_object_show(views);
        elm_table_pack(table, views, VIEWS_COL, 0, 1, 1);
     }
   else
     elm_list_clear(views);

   btn_open_view = elm_button_add(views);
   elm_object_text_set(btn_open_view, "Open a view");
   evas_object_smart_callback_add(btn_open_view, "clicked", _app_view_open, app);
   elm_list_item_append(views, NULL, btn_open_view, NULL, NULL, NULL);

   views_iter = elm_app_client_views_get(app);
   EINA_ITERATOR_FOREACH(views_iter, client_view)
     {
        const char *path = NULL;

        path = elm_app_client_view_path_get(client_view);
        elm_list_item_append(views, path, NULL, NULL, _app_view_clicked, client_view);
     }
   eina_iterator_free(views_iter);

   btn_close_all = elm_button_add(views);
   elm_object_text_set(btn_close_all, "Close all views");
   evas_object_smart_callback_add(btn_close_all, "clicked", _app_close_all_views_cb, app);
   elm_list_item_append(views, NULL, btn_close_all, NULL, NULL, NULL);

   btn_terminate = elm_button_add(views);
   elm_object_text_set(btn_terminate, "Terminate application");
   evas_object_smart_callback_add(btn_terminate, "clicked", _app_terminate_cb, app);
   elm_list_item_append(views, NULL, btn_terminate, NULL, NULL, NULL);

   elm_list_go(views);
}

static void
_view_list_update_cb(void *data EINA_UNUSED, const Eo_Event *event)
{
   if (app_selected == event->object)
     _app_clicked(event->object, NULL, NULL);
}

static void
_win_del(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
{
   Elm_App_Client *app;
   EINA_LIST_FREE(apps_list, app)
     efl_del(app);
}

static void
_app_open(const char *package)
{
   Elm_App_Client *app;
   Eina_List *l;

   EINA_LIST_FOREACH(apps_list, l, app)
     {
        const char *app_package = NULL;

        app_package = elm_app_client_package_get(app);
        if (!app_package)
          return;
        if (!strcmp(package, app_package))
          return;
     }

   app = efl_add(ELM_APP_CLIENT_CLASS, NULL, elm_app_client_constructor(efl_self, package));
   efl_event_callback_add(app, ELM_APP_CLIENT_EVENT_VIEW_LIST_LOADED, _view_list_update_cb, table);
   efl_event_callback_add(app, ELM_APP_CLIENT_EVENT_VIEW_CREATED, _view_list_update_cb, table);
   efl_event_callback_add(app, ELM_APP_CLIENT_EVENT_VIEW_DELETED, _view_list_update_cb, table);
   apps_list = eina_list_append(apps_list, app);
}

static void
_btn_app_open_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *entry = data;
   const char *pkg = elm_object_text_get(entry);

   if (!pkg || !pkg[0])
     return;

   _app_open(pkg);
   _apps_list_update();
}

static void
_open_custom_app_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   elm_list_item_selected_set(event_info, EINA_FALSE);
   elm_object_focus_set(data, EINA_TRUE);
}

static void
_apps_list_update(void)
{
   Evas_Object *entry, *btn_open, *apps;
   Elm_App_Client *app;
   Eina_List *l;

   apps = elm_table_child_get(table, APPS_COL, 0);
   elm_list_clear(apps);

   EINA_LIST_FOREACH(apps_list, l, app)
     {
        const char *app_package = NULL;
        app_package = elm_app_client_package_get(app);
        elm_list_item_append(apps, app_package, NULL, NULL, _app_clicked, app);
     }

   entry = elm_entry_add(apps);
   elm_entry_single_line_set(entry, EINA_TRUE);
   evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, 0.5);

   btn_open = elm_button_add(apps);
   elm_object_text_set(btn_open, "Open");
   evas_object_smart_callback_add(btn_open, "clicked", _btn_app_open_cb, entry);
   elm_list_item_append(apps, NULL, entry, btn_open, _open_custom_app_cb, entry);

   elm_list_go(apps);
}

void
test_task_switcher(void *data EINA_UNUSED,
             Evas_Object *obj EINA_UNUSED,
             void *event_info EINA_UNUSED)
{
   Evas_Object *win, *apps;

   if (apps_list)
     {
        printf("Task switcher already open.\n");
        return;
     }

   win = elm_win_util_standard_add("task switcher", "Task switcher");
   elm_win_autodel_set(win, EINA_TRUE);

   evas_object_smart_callback_add(win, "delete,request", _win_del, NULL);

   table = elm_table_add(win);
   elm_win_resize_object_add(win, table);
   elm_table_padding_set(table, 0, 0);
   elm_table_homogeneous_set(table, EINA_TRUE);
   evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(table, EVAS_HINT_FILL, EVAS_HINT_FILL);

   _app_open("org.enlightenment.message");
   _app_open("org.enlightenment.phone");

   apps = elm_list_add(table);
   evas_object_size_hint_align_set(apps, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_show(apps);
   elm_table_pack(table, apps, APPS_COL, 0, 1, 1);
   _apps_list_update();

   evas_object_show(table);
   evas_object_resize(win, 1024, 768);
   evas_object_show(win);
}