Class: MittensUi::Notify

Inherits:
Core
  • Object
show all
Defined in:
lib/mittens_ui/notify.rb

Overview

A dismissible notification widget that displays styled messages with optional auto-hide timer. Supports multiple notification types (error, info, question, default) with distinct styling.

Examples:

Basic notification

MittensUi::Notify.new("Operation completed successfully!")

Error notification without auto-hide

MittensUi::Notify.new("An error occurred", type: :error, timer: false)

Question notification

MittensUi::Notify.new("Are you sure?", type: :question)

Info notification with custom timer

notify = MittensUi::Notify.new("Processing...", type: :info)
# Notification auto-hides after 8 seconds

Constant Summary collapse

NOTIFICATION_CSS =

Embedded CSS styles for all notification types

<<~CSS
  /* Base notification styles */
  .notify-container {
    padding: 12px;
    margin: 6px 10px;
    border-radius: 6px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    transition: all 0.3s ease;
  }

  /* Default notification */
  .notify-default {
    background-color: #f0f0f0;
    color: #333333;
    border-left: 4px solid #cccccc;
  }

  /* Error notification */
  .notify-error {
    background-color: #ffebee;
    color: #c62828;
    border-left: 4px solid #ef5350;
  }

  /* Info notification */
  .notify-info {
    background-color: #e3f2fd;
    color: #1565c0;
    border-left: 4px solid #42a5f5;
  }

  /* Question notification */
  .notify-question {
    background-color: #f3e5f5;
    color: #6a1b9a;
    border-left: 4px solid #ab47bc;
  }

  /* Close button styles */
  .notify-close-button {
    min-width: 24px;
    min-height: 24px;
    padding: 0;
    margin: 0 0 0 12px;
    border: none;
    background: none;
    color: inherit;
    font-weight: bold;
    //cursor: pointer;
  }

  .notify-close-button:hover {
    background-color: rgba(0,0,0,0.1);
    border-radius: 12px;
  }
CSS

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods inherited from Core

#hidden?, #hide, #keyboard_shortcut, #remove, #remove_keyboard_shortcut, #shortcuts, #show

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Constructor Details

#initialize(msg, options = {}) ⇒ Notify

Creates a new Notify widget with styled notification

Parameters:

  • msg (String)

    the notification message to display

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :timer (Boolean) — default: true

    when true, automatically hides after 8 seconds

  • :type (Symbol) — default: :default

    notification type (:error, :question, :info, :default)

  • :width (Symbol) — default: :full

    column width in layout grid

  • :defer_render (Boolean) — default: false

    when true, skips auto-rendering



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
# File 'lib/mittens_ui/notify.rb', line 90

def initialize(msg, options = {})
  @activate_timer = options.fetch(:timer, true)
  @custom_duration = options.fetch(:duration, 8000)

  # Setup CSS provider
  setup_css_provider

  # Root container
  @notify_bar = Gtk::Box.new(:horizontal, 10)
  @notify_bar.add_css_class('notify-container')
  @notify_bar.add_css_class(css_class_for(options))

  # Message label
  msg_label = Gtk::Label.new(msg)
  msg_label.hexpand = true
  msg_label.xalign = 0

  # Close button
  close_button = Gtk::Button.new(label: '')
  close_button.add_css_class('notify-close-button')
  close_button.signal_connect('clicked') do
    @notify_bar.visible = false
  end

  @notify_bar.append(msg_label)
  @notify_bar.append(close_button)

  super(@notify_bar, options)
end

Instance Method Details

#renderNotify

Displays the notification in the application layout

Returns:

  • (Notify)

    returns self for method chaining



123
124
125
126
127
128
129
# File 'lib/mittens_ui/notify.rb', line 123

def render
  layout = MittensUi::Application.layout
  layout.add_at_top(@notify_bar)
  @notify_bar.visible = true
  trigger_notify_timer if @activate_timer
  self
end