roster/src/widgets/project_item.py
Pavel Baksy ecc9094514 Add left sidebar for managing saved requests in projects
Implement a 3-column layout with a collapsible sidebar for organizing
and managing HTTP requests within projects. Requests are stored in
~/.roster/requests.json for easy git versioning.

Features:
- Create, rename, and delete projects
- Save current request to a project with custom name
- Load saved requests with direct click (no confirmation dialog)
- Delete saved requests with confirmation
- Expandable/collapsible project folders
- Context menu for project actions

Technical changes:
- Add SavedRequest and Project data models with JSON serialization
- Implement ProjectManager for persistence to ~/.roster/requests.json
- Create RequestItem and ProjectItem widgets with GTK templates
- Restructure main window UI to nested GtkPaned (3-column layout)
- Add project management dialogs using AdwAlertDialog
- Update build system with new source files and UI resources
2025-12-18 15:16:21 +01:00

99 lines
3.6 KiB
Python

# project_item.py
#
# Copyright 2025 Pavel Baksy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import Gtk, GObject, Gio
from .request_item import RequestItem
@Gtk.Template(resource_path='/cz/vesp/roster/widgets/project-item.ui')
class ProjectItem(Gtk.Box):
"""Widget for displaying a project with its requests."""
__gtype_name__ = 'ProjectItem'
expand_icon = Gtk.Template.Child()
name_label = Gtk.Template.Child()
requests_revealer = Gtk.Template.Child()
requests_listbox = Gtk.Template.Child()
__gsignals__ = {
'rename-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
'delete-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
'add-request-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
'request-load-requested': (GObject.SIGNAL_RUN_FIRST, None, (object,)),
'request-delete-requested': (GObject.SIGNAL_RUN_FIRST, None, (object,)),
}
def __init__(self, project):
super().__init__()
self.project = project
self.expanded = False
self._setup_actions()
self._populate()
# Click gesture for header
gesture = Gtk.GestureClick.new()
gesture.connect('released', self._on_header_clicked)
self.add_controller(gesture)
def _setup_actions(self):
"""Setup action group for menu."""
actions = Gio.SimpleActionGroup.new()
action = Gio.SimpleAction.new("add-request", None)
action.connect("activate", lambda *_: self.emit('add-request-requested'))
actions.add_action(action)
action = Gio.SimpleAction.new("rename", None)
action.connect("activate", lambda *_: self.emit('rename-requested'))
actions.add_action(action)
action = Gio.SimpleAction.new("delete", None)
action.connect("activate", lambda *_: self.emit('delete-requested'))
actions.add_action(action)
self.insert_action_group("project", actions)
def _populate(self):
"""Populate with project data."""
self.name_label.set_text(self.project.name)
# Clear and add requests
while child := self.requests_listbox.get_first_child():
self.requests_listbox.remove(child)
for saved_request in self.project.requests:
item = RequestItem(saved_request)
item.connect('load-requested',
lambda w, sr=saved_request: self.emit('request-load-requested', sr))
item.connect('delete-requested',
lambda w, sr=saved_request: self.emit('request-delete-requested', sr))
self.requests_listbox.append(item)
def _on_header_clicked(self, gesture, n_press, x, y):
"""Toggle expansion."""
self.expanded = not self.expanded
self.requests_revealer.set_reveal_child(self.expanded)
icon_name = "go-down-symbolic" if self.expanded else "go-next-symbolic"
self.expand_icon.set_from_icon_name(icon_name)
def refresh(self):
"""Refresh from project data."""
self._populate()