1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
|
## Code of Conduct
When interacting with the project, the
[GNOME Code of Conduct](https://conduct.gnome.org/) applies.
## Use of Generative AI
This project does not allow contributions generated by large languages models
(LLMs) and chatbots. This ban includes tools like ChatGPT, Ollama, Claude,
Copilot, DeepSeek, and Devin AI. We are taking these steps as precaution due
to the potential negative influence of AI generated content on quality, as
well as likely copyright violations.
This ban of AI generated content applies to all parts of the projects,
including, but not limited to, code, documentation, issues, and artworks. An
exception applies for purely translating texts for issues and comments to
English.
AI tools can be used to answer questions and find information. However, we
encourage contributors to avoid them in favor of using
[existing documentation](https://developer.gnome.org) and our
[chats and forums](https://welcome.gnome.org). Since AI generated information is
frequently misleading or false, we cannot supply support on anything referencing
AI output.
## Building
For build instructions see the [README.md](./README.md)
## Pull Requests
Before filing a pull request run the tests:
```sh
ninja -C _build test
```
Use descriptive commit messages, see
https://wiki.gnome.org/Git/CommitMessages
and check
https://wiki.openstack.org/wiki/GitCommitMessages
for good examples.
## Coding Style
We mostly use kernel style but
* Use spaces, never tabs
* Use 2 spaces for indentation
### GTK Style Function Argument Indentation
Use GTK style function argument indentation. It's harder for renames but it's
what GNOME upstream projects do.
*Good*:
```c
static gboolean
key_press_event_cb (GtkWidget *widget,
GdkEvent *event,
gpointer data)
```
*Bad*:
```c
static gboolean
key_press_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
```
### Function Prototypes
Function prototypes should be grouped together in logical groups, e.g. all
constructors, or the getter and setter of a given property.
There should be no empty line in a group, and groups should be separated from
each other by an empty line.
The function's attributes and returned type should be on the same line as the
function's name.
Have only one parameter per line.
If a function takes no parameters, explicit it by using `void` as the parameter.
In a group, align function names, the opening parentheses, and parameter names
together.
Use the least possible amount of spaces while preserving alignment.
Asterisks from the returned type should be stuck to the function's name.
Similarly, asterisks from a parameter's type should be stuck to the parameter's
name.
Getters should come before setters.
In public headers, set the function's availability or deprecation state in a
line preceding its prototype.
*Good*:
```c
ADW_AVAILABLE_IN_ALL
AdwFoo *adw_foo_new (void) G_GNUC_WARN_UNUSED_RESULT;
ADW_AVAILABLE_IN_ALL
AdwBar *adw_foo_get_bar (AdwFoo *self);
ADW_AVAILABLE_IN_ALL
void adw_foo_set_bar (AdwFoo *self,
AdwBar *bar);
ADW_AVAILABLE_IN_ALL
gboolean adw_foo_get_bit (AdwFoo *self);
ADW_AVAILABLE_IN_ALL
void adw_foo_set_bit (AdwFoo *self,
gboolean bit);
ADW_AVAILABLE_IN_ALL
void adw_foo_add_baz (AdwFoo *self,
AdwBaz *baz);
ADW_AVAILABLE_IN_ALL
void adw_foo_remove_baz (AdwFoo *self,
AdwBaz *baz);
ADW_AVAILABLE_IN_ALL
void adw_foo_frobnicate (AdwFoo *self);
```
If the function transfers a new handle to a resource, like a reference, a
floating reference, a file handle, or any other kind of handle that would result
into a resource leak if ignored, add `G_GNUC_WARN_UNUSED_RESULT` after the
closing parenthesis.
No need to add `G_GNUC_WARN_UNUSED_RESULT` when the caller is guaranteed to have
a handle to the resource, e.g. in methods incrementing a reference counter.
*Good*:
```c
ADW_AVAILABLE_IN_ALL
AdwFoo *adw_foo_new (void) G_GNUC_WARN_UNUSED_RESULT;
ADW_AVAILABLE_IN_ALL
AdwFoo *adw_foo_ref (AdwFoo *self);
ADW_AVAILABLE_IN_ALL
char *adw_foo_to_string (AdwFoo *self) G_GNUC_WARN_UNUSED_RESULT;
```
*Bad*:
```c
ADW_AVAILABLE_IN_ALL
AdwFoo *adw_foo_new (void);
ADW_AVAILABLE_IN_ALL
AdwFoo *adw_foo_ref (AdwFoo *self) G_GNUC_WARN_UNUSED_RESULT;
ADW_AVAILABLE_IN_ALL
char *adw_foo_to_string (AdwFoo *self);
```
### Braces
Everything besides functions and structs have the opening curly brace on the same line.
*Good*:
```c
if (i < 0) {
...
}
```
*Bad*:
```c
if (i < 0)
{
...
}
```
Single line `if` or `else` statements don't need braces but if either `if` or
`else` have braces both get them:
*Good*:
```c
if (i < 0)
i++;
else
i--;
```
```c
if (i < 0) {
i++;
j++;
} else {
i--;
}
```
```c
if (i < 0) {
i++;
} else {
i--;
j--;
}
```
*Bad*:
```c
if (i < 0) {
i++;
} else {
i--;
}
```
```c
if (i < 0) {
i++;
j++;
} else
i--;
```
```c
if (i < 0)
i++;
else {
i--;
j--;
}
```
Function calls have a space between function name and invocation:
*Good*:
```c
visible_child_name = gtk_stack_get_visible_child_name (GTK_STACK (self->stack));
```
*Bad*:
```c
visible_child_name = gtk_stack_get_visible_child_name(GTK_STACK(self->stack));
```
### Header Inclusion Guards
Guard header inclusion with `#pragma once` rather than the traditional
`#ifndef`-`#define`-`#endif` trio.
Internal headers (for consistency, whether they need to be installed or not)
should contain the following guard to prevent users from directly including
them:
```c
#if !defined(_ADWAITA_INSIDE) && !defined(ADWAITA_COMPILATION)
#error "Only <adwaita.h> can be included directly."
#endif
```
Only after these should you include headers.
### Signals
Prefix signal enum names with *SIGNAL_*.
*Good*:
```c
enum {
SIGNAL_SUBMITTED = 0,
SIGNAL_DELETED,
SIGNAL_SYMBOL_CLICKED,
SIGNAL_LAST_SIGNAL,
};
```
Also note that the last element ends with a comma to reduce diff noise when
adding further signals.
### Properties
Prefix property enum names with *PROP_*.
*Good*:
```c
enum {
PROP_0 = 0,
PROP_NUMBER,
PROP_SHOW_ACTION_BUTTONS,
PROP_COLUMN_SPACING,
PROP_ROW_SPACING,
PROP_RELIEF,
PROP_LAST_PROP,
};
```
Also note that the last element ends with a comma to reduce diff noise when
adding further properties.
### Comment Style
In comments use full sentences with proper capitalization and punctuation.
*Good*:
```c
/* Make sure we don't overflow. */
```
*Bad:*
```c
/* overflow check */
```
### Callbacks
Signal callbacks have a *_cb* suffix.
*Good*:
```c
g_signal_connect(self, "clicked", G_CALLBACK (button_clicked_cb), NULL);
```
*Bad*:
```c
g_signal_connect(self, "clicked", G_CALLBACK (handle_button_clicked), NULL);
```
### Static Functions
Static functions don't need the class prefix. E.g. with a type foo_bar:
*Good*:
```c
static void
selection_changed_cb (AdwViewSwitcher *self,
guint position,
guint n_items)
```
*Bad*:
```c
static void
adw_view_switcher_selection_changed_cb (AdwViewSwitcher *self,
guint position,
guint n_items)
```
Note however that virtual methods like
*<class_name>_{init,constructed,finalize,dispose}* do use the class prefix.
These functions are usually never called directly but only assigned once in
*<class_name>_constructed* so the longer name is kind of acceptable. This also
helps to distinguish virtual methods from regular private methods.
### Self Argument
The first argument is usually the object itself so call it *self*. E.g. for a
non public function:
*Good*:
```c
static gboolean
expire_cb (FooButton *self)
{
g_return_val_if_fail (BAR_IS_FOO_BUTTON (self), FALSE);
...
return FALSE;
}
```
And for a public function:
*Good*:
```c
int
foo_button_get_state (FooButton *self)
{
FooButtonPrivate *priv = bar_foo_get_instance_private(self);
g_return_val_if_fail (BAR_IS_FOO_BUTTON (self), -1);
return priv->state;
}
```
### User Interface Files
User interface files should end in *.ui*. If there are multiple ui
files put them in a ui/ subdirectory below the sources
(e.g. *src/ui/main-window.ui*).
#### Properties
Use minus signs instead of underscores in property names:
*Good*:
```xml
<property name="margin-start">12</property>
```
*Bad*:
```xml
<property name="margin_start">12</property>
```
## Docs
Libadwaita documentation uses gi-docgen and should follow its conventions, as
described in [gi-docgen docs](https://gnome.pages.gitlab.gnome.org/gi-docgen/tutorial.html).
### Summaries
gi-docgen uses the first paragraph of most doc comments as a summary that can be
displayed in a list of symbols. This paragraph must be kept short and to the
point. As a rule of thumb, it should not exceed a single line. It doesn't need
to describe every detail, add more paragraphs for that.
*Good*:
```md
* Sets whether @self can be closed.
*
* If set to `FALSE`, the close button, shortcuts and
* [method@Dialog.close] will result in [signal@Dialog::close-attempt] being
* emitted instead, and bottom sheet close swipe will be disabled.
* [method@Dialog.force_close] still works.
```
*Bad*:
```md
* Sets whether @self can be closed. If set to `FALSE`, the close button,
* shortcuts and [method@Dialog.close] will result in
* [signal@Dialog::close-attempt] being emitted instead, and bottom sheet close
* swipe will be disabled. [method@Dialog.force_close] still works.
```
### GTK-doc syntax
Even though gi-docgen supports legacy gtk-doc syntax within doc comments, avoid
using it and stick to gi-docgen syntax instead.
*Good*:
```md
`TRUE`
[class@Dialog]
[method@Dialog.present]
```
*Bad*:
```md
%TRUE
%AdwDialog
adw_dialog_present()
```
### Parameters and Return Values
gi-docgen already mentions parameter and return value types. Don't mention them
explicitly, instead use more specific descriptions when possible. Even in
obvious cases, such as the `@self` parameter, avoid repetition and omit the
type.
*Good*:
```md
* @self: a window
* @breakpoint: (transfer full): the breakpoint to add
```
*Bad*:
```md
* @self: an `AdwWindow`
* @breakpoint: (transfer full): an `AdwBreakpoint`
```
### Linking to Symbols
When linking to other libadwaita symbols, don't include the namespace when
possible.
*Good*:
```md
[class@NavigationView]
[property@Banner:title]
[method@Dialog.present]
[signal@AlertDialog::response]
[enum@AnimationState]
```
Linking to enum members needs a namespace, see
https://gitlab.gnome.org/GNOME/gi-docgen/-/issues/186
```md
[enum@Adw.AnimationState.PLAYING]
```
*Bad*:
```xml
[class@Adw.NavigationView]
[property@Adw.Banner:title]
[method@Adw.Dialog.present]
[signal@Adw.AlertDialog::response]
[enum@Adw.AnimationState]
```
### Headings
Avoid level 1 headings in doc comments, since gi-docgen will use those for the
symbol itself. Start from level 2.
*Good*:
```md
## CSS Nodes
```
*Bad*:
```md
# CSS Nodes
```
For standalone pages, use level 1 headings anyway. Otherwise, the table of
contents will look wrong.
### Class Descriptions
Try to include an overview of all the functionality the class has, as well as
an example when appropriate.
#### Widgets
Widget documentation should include a screenshot after the summary when
appropriate.
Widgets should also usually include the following sections:
- CSS nodes: describe the widget's CSS name, style classes and internal
structure
- Style classes: if the widget has style classes available for it, list them
here in addition to [doc/style-classes.md](./doc/style-classes.md)
- Accessibility: describe the widget's accessible role here
### Getter, and Setter and Property Descriptions
Getter, setter and property documentation should use the same wording when
possible.
Most of the time, the detailed description is irrelevant for getters, so omit it
and only include the summary.
### Copying From GTK
GTK documentation is less strict about style, so generally speaking docs copied
from GTK need to be reworded and expanded.
### Standalone Pages
Libadwaita has a number of standalone doc pages. Keep them up to date when
making changes.
- [adaptive-layouts.md](./doc/adaptive-layouts.md) lists adaptive patterns with
screenshots and detailed examples. Update it when adding widgets that can be
used for adaptive UIs.
Note: this page lists patterns, not widgets. Use real-world examples. For
example, split views are typically used for sidebars, so even though they don't
provide sidebar contents on their own, include sidebar contents into the
screenshots, rather than leaving the panes empty.
Patterns can also span multiple widgets. For example, `AdwTabView`, `AdwTabBar`,
`AdwTabOverview` and `AdwTabButton` comprise a single pattern and are meant to
be used together, so they are all mentioned in a single section.
- [boxed-lists.md](./doc/boxed-lists.md) lists widgets implementing boxed lists:
list rows, preferences groups etc. Update it when adding new row widgets.
- [css-variables.md](./doc/css-variables.md) lists CSS variables from the
stylesheet, as well as their conventions. Update it whenever you add or change
the variables.
- [style-classes.md](./doc/style-classes.md) lists the available style classes
along with their screenshots, both unique to libadwaita and inherited from
GTK. It should be updated whenever adding a new style class, making an
existing one work for more widgets, or adding a new widget that can use
existing classes.
- [styles-and-appearance.md](./doc/styles-and-appearance.md) lists general
recommendations and guidelines for style handling. It should only be updated
when adding significant new features, such as accent colors.
- [widget-gallery.md](./doc/widget-gallery.md) lists widgets along with their
screenshots. Update it whenever adding a new widget.
### Screenshots
Libadwaita docs use screenshots generated from UI files.
#### Adding a screenshot
1. Create an `IMAGE.ui` file in the `doc/tools/data/` directory.
2. Put the widget to screenshot inside with the `widget` id. For example:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<requires lib="libadwaita" version="1.0"/>
<object class="GtkButton" id="widget">
<property name="label">Example</property>
</object>
</interface>
```
If a widget needs to be hovered - for example, a list item - put the `hover` id
onto it.
If the widget needs special treatment - for example, it's a `GtkPopover` - it
should be special-cased in `screenshot.c` based on its type.
If you need special styles, add them to
[`doc/tools/style.css`](./doc/tools/style.css).
When demoing widgets that don't have a background and may look confusing in the
middle of a doc page, use the `.docs-background` CSS class. It will add a faint
grey background under the screenshot, like in the `AdwStatusPage` screenshot.
3. From the build directory, run:
```
./doc/tools/screenshot ../doc/tools/data/ ../doc/images/ -i IMAGE
```
4. The generator will create `IMAGE.png` and `IMAGE-dark.png` images. Add them
to [`doc/libadwaita.toml.in`](./doc/libadwaita.toml.in).
5. Use them in the docs as follows:
```html
<picture>
<source srcset="IMAGE-dark.png" media="(prefers-color-scheme: dark)">
<img src="IMAGE.png" alt="IMAGE">
</picture>
```
#### Regenerating Screenshots
Make sure your system has Adwaita Sans and Adwaita Mono fonts installed on your
system, otherwise the screenshots may have wrong fonts.
Make sure your system document and monospace fonts are set to:
- `org.gnome.desktop.interface` `document-font-name`: `Adwaita Sans 11`
- `org.gnome.desktop.interface` `monospace-font-name`: `Adwaita Mono 11`
To regenerate all screenshots, run:
```c
./doc/tools/screenshot ../doc/tools/data/ ../doc/images/
```
from the build directory.
|