Skip to content

sentryco/HoverMenu

Repository files navigation

Tests codebeat badge Swift License Platform

HoverMenu

HoverMenu is a Swift package that provides a classic hover menu implementation for both iOS and macOS platforms. It offers a unified API to create context menus that adapt to the specific UI patterns of each operating system.

Note

For iOS, the menu is horizontally arranged (the classic look from UIKit that currently is not available in SwiftUI), and for macOS, the menu is vertically arranged.

Features

  • Platform Support: Fully supports both iOS and macOS, ensuring a consistent experience across devices.
  • Customizable Menus: Offers extensive customization options for menu items including actions, icons, and optional keyboard shortcuts.
  • SwiftUI Integration: Seamlessly integrates with SwiftUI, allowing for modern, declarative UI code.

Usage Example

To use HoverMenu in your SwiftUI application, you can follow this simple example:

import SwiftUI
import HoverMenu

struct ContentView: View {
    var body: some View {
        Text("Right-click or tap to see the menu")
            .contextMenu(menuItems: menuItems, showMenu: $showMenu)
    }

    @State private var showMenu: Bool = false

    var menuItems: MenuItems {
        [
            MenuItem(title: "Refresh", action: refreshAction, iconName: "arrow.clockwise"),
            MenuItem(title: "Settings", action: settingsAction, iconName: "gearshape"),
            MenuItem(title: "Quit", action: quitAction, iconName: "power")
        ]
    }

    func refreshAction() {
        print("Refreshed")
    }

    func settingsAction() {
        print("Settings opened")
    }

    func quitAction() {
        print("Application quitting")
    }
}

Note

.editMenu: For iOS-specific implementations. .popupMenu: For macOS-specific implementations. .contextMenu: A unified modifier that adapts to the platform.

API Usage

HoverMenu provides several modifiers to integrate custom menus into your SwiftUI views:

  • editMenu: Use this modifier for iOS-specific horizontal menus.

    Text("Tap to see the menu")
        .editMenu {
            [
                UIAction(title: "Action 1", handler: { _ in action1() }),
                UIAction(title: "Action 2", handler: { _ in action2() })
            ]
        }
  • popupMenu: Use this modifier for macOS-specific popup menus.

    @State private var showMenu: Bool = false
    
    Button("Show Menu") {
        showMenu = true
    }
    .popupMenu(isPresented: $showMenu, items: menuItems)
  • contextMenu: Use this modifier for a unified API that adapts to the platform.

    Text("Right-click or tap to see the menu")
        .contextMenu(menuItems: menuItems, showMenu: $showMenu)

Installation

  • Add HoverMenu to your project using Swift Package Manager. In Xcode, go to File > Swift Packages > Add Package Dependency and enter the repository URL: https://github.com/sentryco/HoverMenu.git

  • For package.swift file add the following dependency:

dependencies: [
    .package(url: "https://github.com/sentryco/HoverMenu", branch: "main")
]

Todo:

  • Add preview for macOS
  • Rename repo to PopupMenu or PopoverMenu ?
  • Refactoring and Code Cleanup
  • Testing and Documentation (Improving tests and documentation can help maintain high code quality and ease future development efforts.)
  • Code Organization and Structure: Separation of Concerns: Some files contain multiple responsibilities, which could be separated into more focused components or extensions. For example, the PopupMenu.swift file handles UI interactions and also manages state and UI updates, which could be more cleanly separated.
  • Refactoring and Code Cleanup: PopupMenu and EditMenu Classes: There are multiple Fixme comments indicating areas that need attention, such as refactoring and cleaning up the code. For instance, in PopupMenu.swift and EditMenu+Coordinator.swift, there are suggestions to investigate and possibly refactor the way menus are presented and how gestures are handled.
  • Remove unit-tests, add uitests
  • UI/UX Enhancements: User Interface Polish: Enhancing the user interface, such as adding icons to menu items or improving the layout and responsiveness of UI components.
  • reorg project files?
Sources/
    HoverMenu/
        Components/
        EditMenu/
            EditMenu.swift
            EditMenu+Coordinator.swift
            EditMenu+Preview.swift
            EditMenu+UIViewRepresentable.swift
        PopupMenu/
            PopupMenu.swift
            PopupMenu+Const.swift
            PopupMenu+Preview.swift
            PopupMenuWrapper.swift
        Extensions/
        ForEach+Ext.swift
        View+EditMenu.swift
        View+PopupMenu.swift
        Utilities/
        MenuItem.swift
        MenuItems.swift
        MenuItems+Action.swift
        MenuItems+Button.swift
    Tests/
    HoverMenuTests/

About

Classic hover menu for iOS and macOS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages