44
55#include < qatomic.h>
66#include < qdebug.h>
7+ #include < qfilesystemwatcher.h>
78#include < qlogging.h>
89#include < qmutex.h>
910#include < qobject.h>
@@ -187,13 +188,13 @@ class FileView: public QObject {
187188 // / > [!WARNING] We cannot think of a valid use case for this.
188189 // / > You almost definitely want @@blockLoading.
189190 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
191192 // / UI thread until the write succeeds or fails.
192193 // /
193194 // / > [!WARNING] Blocking operations should be used carefully to avoid stutters and other performance
194195 // / > degradations. Blocking means that your interface **WILL NOT FUNCTION** during the call.
195196 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,
197198 // / meaning if the write fails for any reason, the file will not be modified.
198199 // /
199200 // / > [!NOTE] This works by creating another file with the desired content, and renaming
@@ -202,6 +203,18 @@ class FileView: public QObject {
202203 // / If true (default), read or write errors will be printed to the quickshell logs.
203204 // / If false, all known errors will not be printed.
204205 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);
205218
206219 QSDOC_HIDE Q_PROPERTY (QString __path READ path WRITE setPath NOTIFY pathChanged);
207220 QSDOC_HIDE Q_PROPERTY (QString __text READ text NOTIFY internalTextChanged);
@@ -297,6 +310,7 @@ class FileView: public QObject {
297310 [[nodiscard]] QBindable<bool > bindableAtomicWrites () { return &this ->bAtomicWrites ; }
298311
299312 [[nodiscard]] QBindable<bool > bindablePrintErrors () { return &this ->bPrintErrors ; }
313+ [[nodiscard]] QBindable<bool > bindableWatchChanges () { return &this ->bWatchChanges ; }
300314
301315signals:
302316 // / Emitted if the file was loaded successfully.
@@ -307,6 +321,8 @@ class FileView: public QObject {
307321 void saved ();
308322 // / Emitted if the file failed to save.
309323 void saveFailed (qs::io::FileViewError::Enum error);
324+ // / Emitted if the file changes on disk and @@watchChanges is true.
325+ void fileChanged ();
310326
311327 void pathChanged ();
312328 QSDOC_HIDE void internalTextChanged ();
@@ -320,6 +336,7 @@ class FileView: public QObject {
320336 void blockWritesChanged ();
321337 void atomicWritesChanged ();
322338 void printErrorsChanged ();
339+ void watchChangesChanged ();
323340
324341private slots:
325342 void operationFinished ();
@@ -332,6 +349,9 @@ private slots:
332349 void saveSync ();
333350 void updateState (FileViewState& newState);
334351 void updatePath ();
352+ void updateWatchedFiles ();
353+ void onWatchedFileChanged ();
354+ void onWatchedDirectoryChanged ();
335355
336356 [[nodiscard]] bool shouldBlockRead () const ;
337357 [[nodiscard]] FileViewReader* liveReader () const ;
@@ -353,6 +373,8 @@ private slots:
353373 bool mBlockLoading = false ;
354374 bool mBlockAllReads = false ;
355375
376+ QFileSystemWatcher* watcher = nullptr ;
377+
356378 GuardedEmitter<&FileView::internalTextChanged> textChangedEmitter;
357379 GuardedEmitter<&FileView::internalDataChanged> dataChangedEmitter;
358380 void emitDataChanged ();
@@ -374,8 +396,11 @@ private slots:
374396 Q_OBJECT_BINDABLE_PROPERTY (FileView, bool , bBlockWrites, &FileView::blockWritesChanged);
375397 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS (FileView, bool , bAtomicWrites, true , &FileView::atomicWritesChanged);
376398 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS (FileView, bool , bPrintErrors, true , &FileView::printErrorsChanged);
399+ Q_OBJECT_BINDABLE_PROPERTY (FileView, bool , bWatchChanges, &FileView::watchChangesChanged);
377400 // clang-format on
378401
402+ QS_BINDING_SUBSCRIBE_METHOD (FileView, bWatchChanges, updateWatchedFiles, onValueChanged);
403+
379404 void setPreload (bool preload);
380405 void setBlockLoading (bool blockLoading);
381406 void setBlockAllReads (bool blockAllReads);
0 commit comments