Toga

Toga

Feb 28, 2025

Contributing to an open-source project

The Project

Toga is a native, cross platform GUI toolkit for Python that enables developers to build beautiful user interfaces for desktop, mobile, and web applications with a single, Pythonic API. As part of a Software Engineering course, we chose to contribute to this open source project by tackling a real issue that affected developers using the framework.

image

The Challenge

The write_text and measure_text methods in Toga's Canvas API supported multi line text, but the spacing between lines couldn't be customized. This limitation meant developers had no control over text density, which is often critical for designing responsive and accessible interfaces. When you're building applications that need precise typography control, this becomes a real problem.

We identified this as a valuable contribution opportunity: implementing line height control would give developers the flexibility they need while maintaining Toga's simple, Pythonic API.

Our Solution

We implemented a new line_height parameter for the Canvas text methods, allowing developers to specify line spacing as a ratio of the font size, much like the familiar CSS line height property. This gives precise control over text density while keeping the API intuitive.

The implementation required consistent support across all of Toga's backends: macOS using Cocoa, Windows using WinForms, Linux using GTK, and mobile platforms Android and iOS. Each platform has its own text rendering system, so we needed to understand how each one handles line spacing and adapt our implementation accordingly.

We added an interactive slider to the canvas testbed to demonstrate dynamic line height adjustment, making it easy for developers to see the feature in action. We also ensured thorough testing on all supported platforms and updated the documentation to reflect the new capability.

The Experience

Contributing to Toga was a great introduction to open source development. Working with the BeeWare community, particularly with guidance from @freakboy3742, taught us about the importance of cross platform compatibility and maintaining API consistency. Collaborating with @amaekh, @Trighap52, @karindev, and @HaraldHR showed us how effective teamwork can turn a complex problem into a clean solution.

The pull request was accepted and merged, and the feature is now available to all Toga developers. It's satisfying to know that our contribution helps other developers build better interfaces.

Check out Toga and explore our pull request.