Skip to content

Latest commit

 

History

History
197 lines (139 loc) · 17.8 KB

Development.md

File metadata and controls

197 lines (139 loc) · 17.8 KB

Development Notes

Language: English | 简体中文

This document records the project structure of QWindowKit, the detailed conventions, the design rationale of the build system, the test approach, and known issues. The collaboration rules are in AGENTS.md at the repository root.

Project Structure

QWindowKit replaces the system title bar with a title bar drawn by the application, while preserving system behaviors such as window moving, resizing, the system menu, and Snap Layout on Windows. The project provides three libraries: QWKCore, QWKWidgets, and QWKQuick.

Directory Contents
src/core QWKCore, containing the platform-independent parts and one window context per platform
src/core/shared Small components shared across platforms
src/widgets QWKWidgets, a thin layer that connects the core agent to QWidget
src/quick QWKQuick, a thin layer that connects the core agent to QQuickWindow, independent of src/widgets
cmake Build functions, one feature per file
share Integration files for users who do not use CMake, namely the qmake .pri and the Visual Studio .props, generated by share/install.cmake at install time
examples Runnable examples, which use their own qwk_add_example instead of the build functions of src/
tests/buildsystems Consumer build tests, described in "Tests"
tests/auto, tests/manual Empty, reserved for test cases of the libraries
docs Tutorials, the to-do list, and this document

Regular builds do not involve the files in share/. Only tests/buildsystems/ uses these files.

Window Contexts

Platform differences are concentrated in the implementations of AbstractWindowContext, located in src/core/contexts/.

Platform Implementation
Windows win32windowcontext.cpp
macOS cocoawindowcontext.mm
Linux X11 linuxx11context.cpp
Linux Wayland linuxwaylandcontext.cpp
Fallback qtwindowcontext.cpp, emulated with Qt only

Build System

All functions required by the build are located in cmake/. src/conf.cmake only sets project constants and finds Qt in directory scope. Configuration requires no external CMake package and no submodule.

The root of the build output is QWINDOWKIT_BUILD_DIR. If the variable is undefined, the root CMakeLists.txt takes QWINDOWKIT_BUILD_BASE_DIR, then QMSETUP_BUILD_DIR, and otherwise <build directory>/out. The former two variables are accepted for existing parent projects. src/conf.cmake derives QWINDOWKIT_BUILD_RUNTIME_DIR, QWINDOWKIT_BUILD_LIBRARY_DIR, QWINDOWKIT_BUILD_INCLUDE_DIR, QWINDOWKIT_BUILD_DATA_DIR, and QWINDOWKIT_BUILD_DOC_DIR from the root for the library targets, and a value supplied by the parent project or the cache takes precedence. The examples use the CMAKE_*_OUTPUT_DIRECTORY of the parent project if defined, and the directories under the root otherwise.

Organization of CMake Functions

New or replaced CMake functions follow the convention of stdware/stdcorelib. Each file provides one feature and is placed in cmake/ at the repository root. The file name is in upper camel case and named after the feature, for example AddWinRC.cmake and AddInstallPackageRule.cmake. The root CMakeLists.txt includes each file explicitly with include(). cmake/ is not installed with the package, because consumers do not call the build functions of this repository.

Functions and macros are not defined in src/CMakeLists.txt or in the CMakeLists.txt of a module. A helper that generates a whole set of functions as a replacement for qmsetup is not written either.

Generated Files

A generated file must not be rewritten if its content is unchanged, because every translation unit that depends on the file is otherwise recompiled. The implementation reads the existing file, compares the content, and calls file(WRITE) only if the content differs. The directory is not deleted and rewritten as a whole. file(CONFIGURE) and configure_file() write a temporary file before comparing, which took 6.6 s for 2000 files in a measurement. These commands are therefore not used for bulk generation.

CMake File Header

Each file begins with a #[[ ... ]] block that specifies the calling form and the preconditions. The first line starts with a verb and describes the feature. The calling form follows, with optional arguments in square brackets. The remaining text specifies default values, the resolution of relative paths, the cases without effect, and the reasons for these cases. If the file depends on other files in cmake/, a final Requires line lists each dependency. cmake/AddWinRC.cmake serves as the example:

#[[
# Adds Windows version information and an optional icon to an executable or a shared library.
#
# qwk_add_win_rc(<target>
#     [NAME <name>]
#     [VERSION <version>]
#     [DESCRIPTION <description>]
#     [COPYRIGHT <copyright>]
#     [ICON <ico>]
# )
#
# NAME defaults to the target name, VERSION to PROJECT_VERSION, DESCRIPTION to
# PROJECT_DESCRIPTION, and COPYRIGHT to NAME. A relative ICON is resolved against the current
# source directory. The function has no effect on other platforms and on other target types,
# because a static library has no binary of its own to carry the resource.
#
# A changed icon compiles the resource again at the next build without a configure.
#
# Requires cmake/WriteIfDifferent.cmake.
#]]

Argument Parsing

Before cmake_parse_arguments, three lines set(options ...), set(oneValueArgs ...), and set(multiValueArgs ...) declare the keywords, which are then passed as "${options}" "${oneValueArgs}" "${multiValueArgs}". The rule applies to both functions and macros. The prefix is always FUNC. A non-empty FUNC_UNPARSED_ARGUMENTS is rejected with FATAL_ERROR, and a missing required argument is reported in the same way. The reason is given in the entry about cmake_parse_arguments in "Known Issues".

Options and Assignments

Options are defined in the root CMakeLists.txt with the QWINDOWKIT_ prefix. Default values must preserve the existing behavior of a configuration without any arguments.

A function and a non-obvious assignment carry a comment above them that states their purpose and the reason why the default value is not used. Every explicit assignment in src/conf.cmake has a reason in its comment, which must be read before the assignment is removed.

Variable expansions used as arguments are quoted. Paths written to disk at configure time contain no generator expressions. qwk_configure_target does not call find_package. Checks test the platform instead of the compiler. The reasons are given in "Known Issues".

Install Layout

The install layout is a commitment to consumers. Downstream find_package, .pri, and .props depend on the paths in the install tree. After a change to the build system, the install tree must therefore be compared file by file. A successful build alone is not sufficient.

C++

General

  • The namespace is QWK.
  • PImpl follows the Qt pattern of Q_DECLARE_PRIVATE with d_ptr.
  • Platform-specific implementations reside in their own files instead of accumulating #ifdef blocks in one file. Small components shared across platforms reside in src/core/shared/.
  • clang-format handles the formatting: 100 columns, 4-space indentation, and indentation inside namespaces. Code is not formatted by hand. clang-format runs only on changed files, and sed -i is never applied to a whole directory.

Export Macros

The export macros are defined in the global header of each module. qwk_add_library defines the two compile definitions <prefix>_LIBRARY and <prefix>_STATIC according to MACRO_PREFIX.

Module Header Export macro Compile definitions
QWKCore src/core/qwkglobal.h QWK_CORE_EXPORT QWK_CORE_LIBRARY, QWK_CORE_STATIC
QWKWidgets src/widgets/qwkwidgetsglobal.h QWK_WIDGETS_EXPORT QWK_WIDGETS_LIBRARY, QWK_WIDGETS_STATIC
QWKQuick src/quick/qwkquickglobal.h QWK_QUICK_EXPORT QWK_QUICK_LIBRARY, QWK_QUICK_STATIC

Private Headers

Private classes and private headers end with _p.h. Private headers are installed under include/QWindowKit/<module>/private/. A change to a private header therefore affects consumers as well.

Compile-Time Switches

Compile-time switches are declared in src/CMakeLists.txt and generated into QWKCore/qwkconfig.h with the value 1 or -1. Sources read a switch with #if QWINDOWKIT_CONFIG(ENABLE_XXX) and do not bypass the macro with #ifdef. QWINDOWKIT_CONFIG is defined in qwkglobal.h and expands to ((1 / QWINDOWKIT_##feature) == 1), which produces an error for an undefined name.

Naming

Item Rule Example
Build function qwk_ prefix, including utility functions used only by other files in cmake/ qwk_add_win_rc
File-internal build function _qwk_ prefix, called only within the defining file _qwk_configure_target_include_qt_private in cmake/ConfigureTarget.cmake
Option and cache variable QWINDOWKIT_ prefix QWINDOWKIT_BUILD_QUICK
Preprocessor macro Prefix by module QWK_CORE, QWK_WIDGETS, QWK_QUICK
Class Upper camel case, Private suffix for private classes WidgetWindowAgentPrivate
Member Lower camel case systemButtonAreaWidget
Private header _p.h suffix widgetwindowagent_p.h
File in cmake/ Upper camel case, named after the feature AddWinRC.cmake
Document in docs/ Upper camel case with an optional .<lang> suffix. TODO.md, README.<lang>.md, and AGENTS.<lang>.md are exceptions VisualStudioGuide.md, Tutorial.md, Tutorial.zh-CN.md

<lang> is a language tag such as zh-CN. A document without a language tag is the English version.

The qmsetup prefixes qm_ and _qm_ and the helper-internal prefix _repo_ must not appear in this repository again.

Comments

  • Documentation comments of the public interface use qdoc style: /*! ... */ above the definition in the .cpp file, with 4-space indentation inside the block, parsed and published by Doxygen. A class description begins with \class and \brief, and a declaration without a definition to attach to uses \fn. Parameters use \a, identifiers use \c, and references to other declarations use \sa. Documentation comments are not migrated to the LLVM /// style, and new documentation comments follow the existing ones.
  • Public headers contain no documentation comments. Content that callers act upon belongs in the qdoc block. Implementation rationale is written with // inside the implementation, or omitted.
  • Comments contain no dashes and do not join clauses with semicolons. A sentence is broken instead. Comments use American spelling.
  • Content that a few words can express is not written as a paragraph. Comments state constraints, ownership, lifetime, and the reason why the implementation can only be as it is. Comments do not repeat what the signature already states, and do not split one sentence into three.
  • Comments are not archaeological. A comment keeps the reason why the current implementation must be as it is, and drops accounts of former implementations and later corrections. Comments that record former implementation errors, and rejected approaches with a clear and important reason, are kept, as described in "Writing Style" of AGENTS.md.
  • Long parts of design rationale belong in docs/, not in public qdoc blocks.
  • Conventions common to the whole project are written in AGENTS.md or in this document and are not explained again at each place of use.

Tests

QWINDOWKIT_BUILD_TESTS enables the tests, which CTest runs.

tests/buildsystems/ contains consumer build tests. Each test first installs a copy into a prefix directory in the build tree, then builds a small program with CMake, qmake, and MSBuild respectively and links the program against the install tree. The tests verify the paths and library names in the package configuration, the .pri, and the .props.

Rules

  • Paths, tool locations, and configurations are always passed with -D and are never hardcoded in test scripts.
  • Each test case uses its own working directory in the build tree, cleared before the run. Test cases share no state.
  • If a platform or tool is unavailable, the test is skipped with the reason stated. Platform checks are not hardcoded, and test cases that always fail are not kept.
  • Untested content and the reasons are recorded in docs/TODO.md.
  • An assertion is classified as checking either the current behavior or the design intent.
  • After a behavior is tested, the defect is restored to confirm that the test fails, by changing one expectation or passing a wrong path deliberately, then rebuilding, running, and reverting.

Rationale

Rule Rationale
Confirm that the build exit code is 0 before confirming that tests pass If the build fails, ctest runs the program from the previous build and reports a false pass
Confirm that the assertions of a new test are executed An empty test set also reports a pass
After testing a behavior, restore the defect and confirm that the test fails A passing test is not necessarily an effective test, because the test can encode the defect
After a bulk change, verify the number of changed occurrences grep -o <pattern> | wc -l counts occurrences, while grep -c counts lines

Known Issues

CMake

  • cmake_parse_arguments does not report unknown keywords and collects them in UNPARSED_ARGUMENTS. A misspelled keyword during a build API migration therefore silently drops the corresponding behavior. Checking UNPARSED_ARGUMENTS detects only part of these errors: a misspelled keyword after a multi-value keyword becomes a value of that keyword and does not enter UNPARSED_ARGUMENTS. In a measurement on 2026-11-04, QT_LINK after SOURCES in a call to qwk_add_example was merged into SOURCES, and target_sources reported the error as a missing source file QT_LINK. In multi-value keywords whose values receive no further check, such as PREDEFINED, this kind of error remains undetected.
  • An unquoted variable expansion is split by argument parsing. If VAR in DESCRIPTION ${VAR} contains spaces, the value is split into multiple arguments, and a oneValueArgs keyword receives only the first word. The description in the Windows resources was once reduced to its first word for this reason and remained unnoticed for a long time.
  • out/include/QWKCore/ in the build tree holds both the forwarding headers and qwkconfig.h. qwk_sync_headers deletes only the forwarding headers written in its previous run and never clears the directory. The former qm_sync_include(... FORCE) deleted the whole directory, which deleted qwkconfig.h and rewrote it at every configure, causing 6 translation units to recompile after every reconfigure.
  • Paths written to disk at configure time must not contain generator expressions, because the generated directory names otherwise contain angle brackets. The default QMSETUP_BUILD_DIR of qmsetup contained $<CONFIG>, which once required reverting a branch. The forwarding headers, the configuration header, and the qmake and MSBuild files under QWINDOWKIT_BUILD_DIR are all written at configure time. The root CMakeLists.txt therefore rejects a value that contains generator expressions.
  • qwk_configure_target is a function and must not call find_package. The Qt 5 private header directory variables exist only in the scope of the find_package call that creates the corresponding target. A lookup inside a function loses the variables when the function returns but keeps the target. A second target therefore neither repeats the lookup nor obtains the variables. Qt modules are found in the directory scope of the directory that uses them or of one of its parents: the libraries in src/conf.cmake, the examples in examples/CMakeLists.txt, and the tests in tests/buildsystems/CMakeLists.txt. The imported targets and QT_VERSION_MAJOR found in src/ do not propagate to sibling directories, therefore each of the three locations performs its own lookup. qwk_configure_target reports an error for a module in QT_LINKS that is not found, and falls back to the private header directories if a Private module is not found.
  • CMake checks must test the platform instead of the compiler. If clang targets x86_64-pc-windows-msvc, both MSVC and MINGW are false in CMake, and non-Windows options in an else() branch are applied incorrectly.
  • On Windows, CMake file(WRITE) writes CRLF in text mode, and file(READ) converts the line endings back to LF. The read-and-compare of generated files therefore still holds on Windows, and the generated headers and the output of qmcorecmd are CRLF as well. A comparison of two generated files must apply the same line-ending handling to both sides.

Windows Toolchain

  • Do not compose cmd /c "xxx.bat && ..." in a non-interactive shell. Git Bash or PowerShell splits the quoted string again, cmd receives an incomplete command, opens an interactive shell, and blocks. Write the commands into a .bat file and run the file instead.
  • Do not write Chinese text in .bat files. cmd reads the file in the ANSI code page, which decodes UTF-8 comments into garbled text that is executed as commands.
  • Qt headers require /Zc:__cplusplus and /permissive-. The qmake mkspec adds both options automatically. A handwritten vcxproj or other MSBuild project must add them explicitly, otherwise compilation fails inside the Qt headers.

Text Processing

  • sed -i converts CRLF to LF. Check the line endings with grep -qU $'\r' before a change, and use an editing tool for CRLF files.
  • Every sed substitution must carry a line address, otherwise the substitution applies globally, and later expressions match text already changed by earlier ones.