4
4
5
5
#include < qatomic.h>
6
6
#include < qdebug.h>
7
+ #include < qfilesystemwatcher.h>
7
8
#include < qlogging.h>
8
9
#include < qmutex.h>
9
10
#include < qobject.h>
@@ -187,13 +188,13 @@ class FileView: public QObject {
187
188
// / > [!WARNING] We cannot think of a valid use case for this.
188
189
// / > You almost definitely want @@blockLoading.
189
190
QSDOC_PROPERTY_OVERRIDE (bool blockAllReads READ blockAllReads WRITE setBlockAllReads NOTIFY blockAllReadsChanged);
190
- // / If true (default false), all calls to @@setText or @@setData will block the
191
+ // / If true (default false), all calls to @@setText() or @@setData() will block the
191
192
// / UI thread until the write succeeds or fails.
192
193
// /
193
194
// / > [!WARNING] Blocking operations should be used carefully to avoid stutters and other performance
194
195
// / > degradations. Blocking means that your interface **WILL NOT FUNCTION** during the call.
195
196
Q_PROPERTY (bool blockWrites READ default WRITE default NOTIFY blockWritesChanged BINDABLE bindableBlockWrites);
196
- // / If true (default), all calls to @@setText or @@setData will be performed atomically,
197
+ // / If true (default), all calls to @@setText() or @@setData() will be performed atomically,
197
198
// / meaning if the write fails for any reason, the file will not be modified.
198
199
// /
199
200
// / > [!NOTE] This works by creating another file with the desired content, and renaming
@@ -202,6 +203,18 @@ class FileView: public QObject {
202
203
// / If true (default), read or write errors will be printed to the quickshell logs.
203
204
// / If false, all known errors will not be printed.
204
205
QSDOC_PROPERTY_OVERRIDE (bool printErrors READ default WRITE default NOTIFY printErrorsChanged);
206
+ // / If true (defaule false), @@fileChanged() will be called whenever the content of the file
207
+ // / changes on disk, including when @@setText() or @@setData() are used.
208
+ // /
209
+ // / > [!NOTE] You can reload the file's content whenever it changes on disk like so:
210
+ // / > ```qml
211
+ // / > FileView {
212
+ // / > // ...
213
+ // / > watchChanges: true
214
+ // / > onFileChanged: this.reload()
215
+ // / > }
216
+ // / > ```
217
+ Q_PROPERTY (bool watchChanges READ default WRITE default NOTIFY watchChangesChanged BINDABLE bindableWatchChanges);
205
218
206
219
QSDOC_HIDE Q_PROPERTY (QString __path READ path WRITE setPath NOTIFY pathChanged);
207
220
QSDOC_HIDE Q_PROPERTY (QString __text READ text NOTIFY internalTextChanged);
@@ -297,6 +310,7 @@ class FileView: public QObject {
297
310
[[nodiscard]] QBindable<bool > bindableAtomicWrites () { return &this ->bAtomicWrites ; }
298
311
299
312
[[nodiscard]] QBindable<bool > bindablePrintErrors () { return &this ->bPrintErrors ; }
313
+ [[nodiscard]] QBindable<bool > bindableWatchChanges () { return &this ->bWatchChanges ; }
300
314
301
315
signals:
302
316
// / Emitted if the file was loaded successfully.
@@ -307,6 +321,8 @@ class FileView: public QObject {
307
321
void saved ();
308
322
// / Emitted if the file failed to save.
309
323
void saveFailed (qs::io::FileViewError::Enum error);
324
+ // / Emitted if the file changes on disk and @@watchChanges is true.
325
+ void fileChanged ();
310
326
311
327
void pathChanged ();
312
328
QSDOC_HIDE void internalTextChanged ();
@@ -320,6 +336,7 @@ class FileView: public QObject {
320
336
void blockWritesChanged ();
321
337
void atomicWritesChanged ();
322
338
void printErrorsChanged ();
339
+ void watchChangesChanged ();
323
340
324
341
private slots:
325
342
void operationFinished ();
@@ -332,6 +349,9 @@ private slots:
332
349
void saveSync ();
333
350
void updateState (FileViewState& newState);
334
351
void updatePath ();
352
+ void updateWatchedFiles ();
353
+ void onWatchedFileChanged ();
354
+ void onWatchedDirectoryChanged ();
335
355
336
356
[[nodiscard]] bool shouldBlockRead () const ;
337
357
[[nodiscard]] FileViewReader* liveReader () const ;
@@ -353,6 +373,8 @@ private slots:
353
373
bool mBlockLoading = false ;
354
374
bool mBlockAllReads = false ;
355
375
376
+ QFileSystemWatcher* watcher = nullptr ;
377
+
356
378
GuardedEmitter<&FileView::internalTextChanged> textChangedEmitter;
357
379
GuardedEmitter<&FileView::internalDataChanged> dataChangedEmitter;
358
380
void emitDataChanged ();
@@ -374,8 +396,11 @@ private slots:
374
396
Q_OBJECT_BINDABLE_PROPERTY (FileView, bool , bBlockWrites, &FileView::blockWritesChanged);
375
397
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS (FileView, bool , bAtomicWrites, true , &FileView::atomicWritesChanged);
376
398
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS (FileView, bool , bPrintErrors, true , &FileView::printErrorsChanged);
399
+ Q_OBJECT_BINDABLE_PROPERTY (FileView, bool , bWatchChanges, &FileView::watchChangesChanged);
377
400
// clang-format on
378
401
402
+ QS_BINDING_SUBSCRIBE_METHOD (FileView, bWatchChanges, updateWatchedFiles, onValueChanged);
403
+
379
404
void setPreload (bool preload);
380
405
void setBlockLoading (bool blockLoading);
381
406
void setBlockAllReads (bool blockAllReads);
0 commit comments