using Gtk 4.0;
using Adw 1;

template $GhosttySurface: Adw.Bin {
  styles [
    "surface",
  ]

  notify::config => $notify_config();
  notify::mouse-hover-url => $notify_mouse_hover_url();
  notify::mouse-hidden => $notify_mouse_hidden();
  notify::mouse-shape => $notify_mouse_shape();

  Overlay {
    focusable: false;
    focus-on-click: false;

    GLArea gl_area {
      realize => $gl_realize();
      unrealize => $gl_unrealize();
      render => $gl_render();
      resize => $gl_resize();
      hexpand: true;
      vexpand: true;
      focusable: true;
      focus-on-click: true;
      has-stencil-buffer: false;
      has-depth-buffer: false;
      use-es: false;
    }

    [overlay]
    ProgressBar progress_bar_overlay {
      styles [
        "osd",
      ]

      visible: false;
      halign: fill;
      valign: start;
    }

    [overlay]
    $GhosttySurfaceChildExited child_exited_overlay {
      visible: bind template.child-exited;
      close-request => $child_exited_close();
    }

    [overlay]
    $GhosttyResizeOverlay resize_overlay {}

    [overlay]
    Label url_left {
      styles [
        "background",
        "url-overlay",
      ]

      visible: false;
      halign: start;
      valign: end;
      label: bind template.mouse-hover-url;

      EventControllerMotion url_ec_motion {
        enter => $url_mouse_enter();
        leave => $url_mouse_leave();
      }
    }

    [overlay]
    Label url_right {
      styles [
        "background",
        "url-overlay",
      ]

      visible: false;
      halign: end;
      valign: end;
      label: bind template.mouse-hover-url;
    }
  }

  // Event controllers for interactivity
  EventControllerFocus {
    enter => $focus_enter();
    leave => $focus_leave();
  }

  EventControllerKey {
    key-pressed => $key_pressed();
    key-released => $key_released();
  }

  EventControllerMotion {
    motion => $mouse_motion();
    leave => $mouse_leave();
  }

  EventControllerScroll {
    scroll => $scroll();
    scroll-begin => $scroll_begin();
    scroll-end => $scroll_end();
    flags: both_axes;
  }

  GestureClick {
    pressed => $mouse_down();
    released => $mouse_up();
    button: 0;
  }

  DropTarget drop_target {
    drop => $drop();
    actions: copy;
  }
}

IMMulticontext im_context {
  preedit-start => $im_preedit_start();
  preedit-changed => $im_preedit_changed();
  preedit-end => $im_preedit_end();
  commit => $im_commit();
}
