summaryrefslogtreecommitdiff
path: root/src/dged/window.c
blob: 20cb22f4fd0a7046dbc358b8681b8a16523ba640 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
#include "binding.h"
#include "btree.h"
#include "buffer.h"
#include "buffer_view.h"
#include "buffers.h"
#include "command.h"
#include "display.h"
#include "minibuffer.h"

#include <math.h>

enum window_type {
  Window_Buffer,
  Window_HSplit,
  Window_VSplit,
};

struct window {
  uint32_t x;
  uint32_t y;
  uint32_t width;
  uint32_t height;
  enum window_type type;
  struct buffer_view buffer_view;
  struct buffer_view prev_buffer_view;
  bool has_prev_buffer_view;
  struct command_list *commands;
  uint32_t relline;
  uint32_t relcol;
};

BINTREE_ENTRY_TYPE(window_node, struct window);

static struct windows {
  BINTREE(window_node) windows;
  struct window *active;
  struct keymap keymap;
} g_windows;

static struct window g_minibuffer_window;

static struct window g_popup_window = {0};
static bool g_popup_visible = false;

static void buffer_removed(struct buffer *buffer, void *userdata) {
  struct window_node *n = BINTREE_ROOT(&g_windows.windows);
  BINTREE_FIRST(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (window_prev_buffer_view(w)->buffer == buffer) {
      buffer_view_destroy(&w->prev_buffer_view);
      w->has_prev_buffer_view = false;
    }

    if (window_buffer(w) == buffer) {
      if (window_has_prev_buffer_view(w)) {
        window_set_buffer(w, window_prev_buffer_view(w)->buffer);
        buffer_view_destroy(&w->prev_buffer_view);
        w->has_prev_buffer_view = false;
      } else {
        struct buffers *buffers = (struct buffers *)userdata;
        struct buffer *b = buffers_find(buffers, "*messages*");

        if (b != NULL) {
          window_set_buffer(w, b);
        } else {
          b = buffers_first(buffers);

          // if there are no more buffers, not sure what to do?
          if (b != NULL) {
            window_set_buffer(w, b);
          }
        }
        buffer_view_destroy(&w->prev_buffer_view);
        w->has_prev_buffer_view = false;
      }
    }

    BINTREE_NEXT(n);
  }
}

void windows_init(uint32_t height, uint32_t width,
                  struct buffer *initial_buffer, struct buffer *minibuffer,
                  struct buffers *buffers) {
  BINTREE_INIT(&g_windows.windows);

  g_minibuffer_window = (struct window){
      .buffer_view = buffer_view_create(minibuffer, false, false),
      .has_prev_buffer_view = false,
      .x = 0,
      .y = height - 1,
      .height = 1,
      .width = width,
  };

  struct window root_window = (struct window){
      .buffer_view = buffer_view_create(initial_buffer, true, true),
      .has_prev_buffer_view = false,
      .height = height - 1,
      .width = width,
      .x = 0,
      .y = 0,
  };
  BINTREE_SET_ROOT(&g_windows.windows, root_window);
  g_windows.active = &BINTREE_VALUE(BINTREE_ROOT(&g_windows.windows));

  buffers_add_remove_hook(buffers, buffer_removed, buffers);
}

static void window_tree_clear_sub(struct window_node *root_node) {
  struct window_node *n = root_node;
  BINTREE_FIRST(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (w->type == Window_Buffer) {
      buffer_view_destroy(&w->buffer_view);
      if (w->has_prev_buffer_view) {
        buffer_view_destroy(&w->prev_buffer_view);
      }
    }
    BINTREE_NEXT(n);
  }
  BINTREE_FREE_NODES(root_node, window_node);
}

static void window_tree_clear() {
  window_tree_clear_sub(BINTREE_ROOT(&g_windows.windows));
}

void windows_destroy() { window_tree_clear(); }

struct window *root_window() {
  return &BINTREE_VALUE(BINTREE_ROOT(&g_windows.windows));
}

struct window *minibuffer_window() {
  return &g_minibuffer_window;
}

struct window *popup_window() {
  return &g_popup_window;
}

bool popup_window_visible() { return g_popup_visible; }

static void window_tree_resize(struct window_node *root, uint32_t height,
                               uint32_t width) {

  /* due to the way tree traversal works, we need to disconnect the subtree from
   * its potential parent. Otherwise the algorithm will traverse above the root
   * of the subtree. */
  struct window_node *orig_parent = BINTREE_PARENT(root);
  BINTREE_PARENT(root) = NULL;

  struct window *root_window = &BINTREE_VALUE(root);
  uint32_t original_root_width = root_window->width;
  uint32_t original_root_height = root_window->height;
  root_window->width = width;
  root_window->height = height;

  struct window_node *n = root;
  BINTREE_FIRST(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (BINTREE_PARENT(n) != NULL && n != root) {
      if (BINTREE_LEFT(BINTREE_PARENT(n)) == n) {
        // if left child, use scale from root
        w->width = round(((float)w->width / (float)original_root_width) *
                         (float)root_window->width);
        w->height = round(((float)w->height / (float)original_root_height) *
                          (float)root_window->height);
      } else {
        // if right child, fill rest of space after left and parent resize
        struct window *left_sibling =
            &BINTREE_VALUE(BINTREE_LEFT(BINTREE_PARENT(n)));
        struct window *parent = &BINTREE_VALUE(BINTREE_PARENT(n));

        w->width = parent->width;
        w->height = parent->height;
        if (parent->type == Window_HSplit) {
          w->y = parent->y + left_sibling->height;
          w->height -= left_sibling->height;
        } else {
          w->x = parent->x + left_sibling->width;
          w->width -= left_sibling->width;
        }
      }
    }
    BINTREE_NEXT(n);
  }

  BINTREE_PARENT(root) = orig_parent;
}

void windows_resize(uint32_t height, uint32_t width) {
  g_minibuffer_window.width = width;
  g_minibuffer_window.y = height - 1;

  window_tree_resize(BINTREE_ROOT(&g_windows.windows), height - 1, width);
}

void windows_update(void *(*frame_alloc)(size_t), float frame_time) {

  struct window *w = &g_minibuffer_window;
  w->x = 0;
  w->commands = command_list_create(10, frame_alloc, w->x, w->y, "mb-prompt");

  // draw the prompt here to make it off-limits for the buffer/buffer view
  uint32_t prompt_len = minibuffer_draw_prompt(w->commands);
  w->x += prompt_len;
  uint32_t width = prompt_len < w->width ? w->width - prompt_len : 1;

  struct command_list *inner_commands = command_list_create(
      w->height * width, frame_alloc, w->x, w->y, "bufview-mb");

  struct buffer_view_update_params p = {
      .commands = inner_commands,
      .window_id = -1,
      .frame_time = frame_time,
      .width = width,
      .height = w->height,
      .window_x = w->x,
      .window_y = w->y,
      .frame_alloc = frame_alloc,
  };

  buffer_view_update(&w->buffer_view, &p);
  command_list_draw_command_list(w->commands, inner_commands);

  if (g_popup_visible) {
    w = &g_popup_window;

    const uint32_t hpadding = 1;
    const uint32_t border_width = 1;

    bool draw_padding = false, draw_borders = false;

    // first, make sure window fits
    struct window *rw = root_window();
    uint32_t w_x = w->x;
    uint32_t w_y = w->y;
    uint32_t width = w_x + w->width > rw->width ? rw->width - w_x : w->width;
    uint32_t height =
        w_y + w->height > rw->height ? rw->height - w_y : w->height;

    // is there space for padding?
    if (w_x > 1 && w_x + width + hpadding <= rw->width) {
      draw_padding = true;

      --w_x;
      width += hpadding * 2;
    }

    // is there space for borders?
    if (w_y > 1 && w_y + height <= rw->height && w_x > 1 &&
        w_x + width + border_width <= rw->width) {
      draw_borders = true;

      w_x -= border_width;
      // shift text upward
      w_y -= border_width * 2;
      height += border_width * 2;
      width += border_width * 2;
    }

    w->commands = command_list_create(height * width, frame_alloc, w_x, w_y,
                                      "popup-decor");
    uint32_t x = 0, y = 0;
    if (draw_borders) {
      // top
      command_list_draw_repeated(w->commands, x, y, 0x8c94e2, 1);
      command_list_draw_repeated(w->commands, x + 1, y, 0x8094e2,
                                 width - (border_width * 2));
      command_list_draw_repeated(w->commands, x + width - 1, y, 0x9094e2, 1);

      // sides
      for (uint32_t line = y + 1; line < (height + y - border_width); ++line) {
        command_list_draw_repeated(w->commands, x, line, 0x8294e2,
                                   border_width);
        command_list_draw_repeated(w->commands, x + width - border_width, line,
                                   0x8294e2, border_width);
      }

      // bottom
      command_list_draw_repeated(w->commands, x, y + height - border_width,
                                 0x9494e2, 1);
      command_list_draw_repeated(w->commands, x + 1, y + height - border_width,
                                 0x8094e2, width - (border_width * 2));
      command_list_draw_repeated(w->commands, x + width - 1,
                                 y + height - border_width, 0x9894e2, 1);

      x += border_width;
      y += border_width;
    }

    if (draw_padding) {
      for (uint32_t line = y; line < w->height + y; ++line) {
        command_list_draw_repeated(w->commands, x, line, ' ', hpadding);
        command_list_draw_repeated(w->commands, w->width + x + 1, line, ' ',
                                   hpadding);
      }
      x += border_width;
    }

    struct command_list *inner = command_list_create(
        w->height * w->width, frame_alloc, w_x + x, w_y + y, "bufview-popup");

    struct buffer_view_update_params p = {
        .commands = inner,
        .window_id = -1,
        .frame_time = frame_time,
        .width = w->width,
        .height = w->height,
        .window_x = w_x + x,
        .window_y = w_y + y,
        .frame_alloc = frame_alloc,
    };

    buffer_view_update(&w->buffer_view, &p);
    command_list_draw_command_list(w->commands, inner);
  }

  struct window_node *n = BINTREE_ROOT(&g_windows.windows);
  BINTREE_FIRST(n);
  uint32_t window_id = 0;
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (w->type == Window_Buffer) {
      char name[16] = {0};
      snprintf(name, 15, "bufview-%s", w->buffer_view.buffer->name);
      w->commands = command_list_create(w->height * w->width, frame_alloc, w->x,
                                        w->y, name);

      struct buffer_view_update_params p = {
          .commands = w->commands,
          .window_id = window_id,
          .frame_time = frame_time,
          .width = w->width,
          .height = w->height,
          .window_x = w->x,
          .window_y = w->y,
          .frame_alloc = frame_alloc,
      };

      buffer_view_update(&w->buffer_view, &p);
      ++window_id;
    }

    BINTREE_NEXT(n);
  }
}

void windows_render(struct display *display) {
  struct window_node *n = BINTREE_ROOT(&g_windows.windows);
  BINTREE_FIRST(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (w->type == Window_Buffer) {
      display_render(display, w->commands);
    }
    BINTREE_NEXT(n);
  }

  display_render(display, g_minibuffer_window.commands);
  if (g_popup_visible) {
    display_render(display, g_popup_window.commands);
  }
}

struct window_node *find_window(struct window *window) {
  struct window_node *n = BINTREE_ROOT(&g_windows.windows);
  BINTREE_FIRST(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (w == window) {
      return n;
    }
    BINTREE_NEXT(n);
  }

  return NULL;
}

void windows_set_active(struct window *window) {
  if (window == minibuffer_window()) {
    g_windows.active = minibuffer_window();
    return;
  }

  struct window_node *n = find_window(window);
  if (n != NULL) {
    g_windows.active = &BINTREE_VALUE(n);
  }
}

struct window *window_find_by_buffer(struct buffer *b) {
  if (b == window_buffer(minibuffer_window())) {
    return minibuffer_window();
  }

  struct window_node *n = BINTREE_ROOT(&g_windows.windows);
  BINTREE_FIRST(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (window_buffer(w) == b) {
      return w;
    }
    BINTREE_NEXT(n);
  }

  return NULL;
}

struct window *windows_get_active() {
  return g_windows.active;
}

void window_set_buffer(struct window *window, struct buffer *buffer) {
  window_set_buffer_e(window, buffer, true, true);
}

void window_set_buffer_e(struct window *window, struct buffer *buffer,
                         bool modeline, bool line_numbers) {
  if (buffer != window->buffer_view.buffer) {
    if (window->has_prev_buffer_view) {

      if (window->prev_buffer_view.buffer == buffer) {

        struct buffer_view tmp = window->prev_buffer_view;
        window->prev_buffer_view = window->buffer_view;
        window->has_prev_buffer_view = true;
        window->buffer_view = tmp;
        return;

      } else {
        buffer_view_destroy(&window->prev_buffer_view);
      }
    }

    window->prev_buffer_view = window->buffer_view;
    window->has_prev_buffer_view = true;
    window->buffer_view = buffer_view_create(buffer, modeline, line_numbers);
  }
}

struct buffer *window_buffer(struct window *window) {
  return window->buffer_view.buffer;
}

struct buffer_view *window_buffer_view(struct window *window) {
  return &window->buffer_view;
}

struct buffer_view *window_prev_buffer_view(struct window *window) {
  return &window->prev_buffer_view;
}

bool window_has_prev_buffer_view(struct window *window) {
  return window->has_prev_buffer_view;
}

void window_close(struct window *window) {
  // do not want to delete last window
  if (window == root_window()) {
    return;
  }

  struct window_node *to_delete = find_window(window);
  if (to_delete == NULL) {
    return;
  }

  // promote other child to parent
  struct window_node *target = BINTREE_PARENT(to_delete);
  struct window_node *source = BINTREE_RIGHT(target) == to_delete
                                   ? BINTREE_LEFT(target)
                                   : BINTREE_RIGHT(target);

  buffer_view_destroy(&window->buffer_view);
  buffer_view_destroy(&window->prev_buffer_view);
  BINTREE_REMOVE(to_delete);
  BINTREE_FREE_NODE(to_delete);

  BINTREE_VALUE(source).x = BINTREE_VALUE(target).x;
  BINTREE_VALUE(source).y = BINTREE_VALUE(target).y;
  uint32_t target_width = BINTREE_VALUE(target).width;
  uint32_t target_height = BINTREE_VALUE(target).height;

  // copy the node value and set it's children as children of the target node
  BINTREE_VALUE(target) = BINTREE_VALUE(source);
  BINTREE_LEFT(target) = BINTREE_LEFT(source);
  BINTREE_RIGHT(target) = BINTREE_RIGHT(source);

  // adopt the children
  if (BINTREE_HAS_LEFT(source)) {
    BINTREE_PARENT(BINTREE_LEFT(source)) = target;
  }

  if (BINTREE_HAS_RIGHT(source)) {
    BINTREE_PARENT(BINTREE_RIGHT(source)) = target;
  }

  BINTREE_FREE_NODE(source);

  window_tree_resize(target, target_height, target_width);
  BINTREE_FIRST(target);
  windows_set_active(&BINTREE_VALUE(target));
}

void window_close_others(struct window *window) {
  struct window_node *root = BINTREE_ROOT(&g_windows.windows);

  // copy window and make it suitable as a root window
  struct window new_root = *window;
  new_root.x = 0;
  new_root.y = 0;
  new_root.buffer_view = buffer_view_clone(&window->buffer_view);
  new_root.has_prev_buffer_view = false;
  if (window->has_prev_buffer_view) {
    new_root.has_prev_buffer_view = true;
    new_root.prev_buffer_view = buffer_view_clone(&window->prev_buffer_view);
  }
  new_root.width = BINTREE_VALUE(root).width;
  new_root.height = BINTREE_VALUE(root).height;

  window_tree_clear();

  // create new root window
  BINTREE_SET_ROOT(&g_windows.windows, new_root);
  windows_set_active(&BINTREE_VALUE(BINTREE_ROOT(&g_windows.windows)));
}

void window_hsplit(struct window *window, struct window **new_window_a,
                   struct window **new_window_b) {
  struct window_node *n = find_window(window);
  if (n != NULL) {
    struct window w = BINTREE_VALUE(n);

    if (w.type == Window_Buffer) {
      struct window parent = {0};
      parent.type = Window_HSplit;
      parent.x = w.x;
      parent.y = w.y;
      parent.width = w.width;
      parent.height = w.height;
      BINTREE_VALUE(n) = parent;

      /* Reuse the current window as the 'left' child, halving the height */
      w.height /= 2;
      BINTREE_INSERT(n, w);
      *new_window_a = &BINTREE_VALUE(BINTREE_LEFT(n));
      windows_set_active(*new_window_a);

      /* Create a new window for the split, showing the same buffer as the
       * original window.
       */
      struct window new_window = {0};
      new_window.type = Window_Buffer;
      new_window.buffer_view =
          buffer_view_create(w.buffer_view.buffer, true, true);
      buffer_view_goto(&new_window.buffer_view,
                       (struct location){.line = w.buffer_view.dot.line,
                                         .col = w.buffer_view.dot.col});
      if (w.has_prev_buffer_view) {
        new_window.prev_buffer_view = buffer_view_clone(&w.prev_buffer_view);
      }
      new_window.x = w.x;
      new_window.y = w.y + w.height;
      new_window.width = w.width;
      new_window.height = parent.height - w.height;
      BINTREE_INSERT(n, new_window);
      *new_window_b = &BINTREE_VALUE(BINTREE_RIGHT(n));
    }
  }
}

void window_vsplit(struct window *window, struct window **new_window_a,
                   struct window **new_window_b) {
  struct window_node *n = find_window(window);
  if (n != NULL) {
    struct window w = BINTREE_VALUE(n);

    if (w.type == Window_Buffer) {
      /* Create a new split container to use as parent */
      struct window parent = {0};
      parent.type = Window_VSplit;
      parent.x = w.x;
      parent.y = w.y;
      parent.width = w.width;
      parent.height = w.height;
      BINTREE_VALUE(n) = parent;

      /* Reuse the current window as the 'left' child, halving the width */
      w.width /= 2;
      BINTREE_INSERT(n, w);
      *new_window_a = &BINTREE_VALUE(BINTREE_LEFT(n));
      windows_set_active(*new_window_a);

      /* Create a new window for the split, showing the same buffer as the
       * original window.
       */
      struct window new_window = {0};
      new_window.type = Window_Buffer;
      new_window.buffer_view =
          buffer_view_create(w.buffer_view.buffer, true, true);
      buffer_view_goto(&new_window.buffer_view,
                       (struct location){.line = w.buffer_view.dot.line,
                                         .col = w.buffer_view.dot.col});

      if (w.has_prev_buffer_view) {
        new_window.prev_buffer_view = buffer_view_clone(&w.prev_buffer_view);
      }
      new_window.x = w.x + w.width;
      new_window.y = w.y;
      new_window.width = parent.width - w.width;
      new_window.height = w.height;
      BINTREE_INSERT(n, new_window);
      *new_window_b = &BINTREE_VALUE(BINTREE_RIGHT(n));
    }
  }
}

void window_split(struct window *window, struct window **new_window_a,
                  struct window **new_window_b) {
  /* The height * 2 is a horrible hack, we would need to know how big the font
     actually is */
  window->height * 2 > window->width
      ? window_hsplit(window, new_window_a, new_window_b)
      : window_vsplit(window, new_window_a, new_window_b);
}

struct window *windows_focus_next() {
  struct window *active = windows_get_active();
  struct window_node *n = find_window(active);
  BINTREE_NEXT(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (w->type == Window_Buffer) {
      windows_set_active(w);
      return w;
    }
    BINTREE_NEXT(n);
  }

  // we have moved around
  n = BINTREE_ROOT(&g_windows.windows);
  BINTREE_FIRST(n);
  if (n != NULL) {
    windows_set_active(&BINTREE_VALUE(n));
    return &BINTREE_VALUE(n);
  }

  // fall back to root
  windows_set_active(root_window());
  return root_window();
}

struct window *windows_focus(uint32_t id) {
  uint32_t curr_id = 0;

  struct window_node *n = BINTREE_ROOT(&g_windows.windows);
  BINTREE_FIRST(n);
  while (n != NULL) {
    struct window *w = &BINTREE_VALUE(n);
    if (w->type == Window_Buffer) {
      if (curr_id == id) {
        windows_set_active(w);
        return w;
      }
      ++curr_id;
    }
    BINTREE_NEXT(n);
  }

  return NULL;
}

uint32_t window_width(const struct window *window) { return window->width; }

uint32_t window_height(const struct window *window) { return window->height; }

struct window_position window_position(const struct window *window) {
  return (struct window_position){.x = window->x, .y = window->y};
}

void windows_show_popup(uint32_t row, uint32_t col, uint32_t width,
                        uint32_t height) {
  g_popup_window.x = col;
  g_popup_window.y = row;
  g_popup_window.width = width;
  g_popup_window.height = height;
  g_popup_visible = true;
}

void windows_close_popup() { g_popup_visible = false; }